分享

java程序命令行启动方法

 好汉勃士 2021-10-30

    刚开始学习java的朋友一定很想知道除了在IDE中点击运行启动java外还可以怎样启动java程序。在这里我就我自己知道的将启动java的非IDE的方法做个介绍。

    1.ant启动

    ant启动的优点是跨平台,写好了xml文件在windows在linux上都可以跑,但是缺点就是必须安装ant包。如何安装ant请baidu相关文档出来。在这里给出一些基本的用法

     a)新建一个build.xml文件,下面是一个实例,不做详细说明,如拷贝使用请修改响应的项

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <project basedir='.' default='run' name='BHO Video indexing'>  
  3.     <description>Buile the BHO video index</description>  
  4.     <!--Set the Global variable-->  
  5.     <property name='src'  location='src' />  
  6.     <property name='build' location='bin'/>  
  7.     <!-- =================================  
  8.         指定程序运行需要的classpath引用,一般不用更改  
  9.         ================================= -->  
  10.     <path id='classpath'>  
  11.         <pathelement location='bin' />  
  12.         <fileset dir='lib'>  
  13.             <include name='**/*.jar' />  
  14.         </fileset>  
  15.     </path>  
  16.       
  17.     <!-- Clear the directories of the out -->  
  18.     <target name='clear'>  
  19.         <echo>Delete the target directories</echo>  
  20.         <delete dir='bin' />  
  21.     </target>  
  22.       
  23.     <!-- =================================   
  24.           target: init                
  25.          ================================= -->  
  26.     <target name='init' depends='clear' description='Create the bin directory'>  
  27.         <echo>Create the bin directory</echo>  
  28.         <mkdir dir='${build}'/>  
  29.     </target>  
  30.       
  31.     <!-- =================================   
  32.               target: compile                 
  33.          ================================= -->  
  34.     <target name='compile' depends='init' description='Compile the project'>  
  35.         <javac srcdir='${src}'  
  36.          destdir='${build}'  
  37.          classpathref='classpath'  
  38.         />  
  39.         <copy todir='bin'>  
  40.             <fileset dir='src'>  
  41.                 <include name='**/*.properties'/>  
  42.             </fileset>  
  43.         </copy>  
  44.     </target>  
  45.       
  46.     <!-- =================================   
  47.           target: run                
  48.          ================================= -->  
  49.     <target name='run' depends='compile' description='run the project'>  
  50.         <java classpathref='classpath' fork='true' classname='<span style='color:#ff0000;'>video.info.Ku6</span>  
  51.   
  52. >  
  53.             <jvmarg value='-Xms400M' />  
  54.             <jvmarg value='-Xmx1024M' />  
  55.         </java>     
  56.     </target>  
  57. </project>  
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <project basedir='.' default='run' name='BHO Video indexing'>  
  3.     <description>Buile the BHO video index</description>  
  4.     <!--Set the Global variable-->  
  5.     <property name='src'  location='src' />  
  6.     <property name='build' location='bin'/>  
  7.     <!-- =================================  
  8.         指定程序运行需要的classpath引用,一般不用更改  
  9.         ================================= -->  
  10.     <path id='classpath'>  
  11.         <pathelement location='bin' />  
  12.         <fileset dir='lib'>  
  13.             <include name='**/*.jar' />  
  14.         </fileset>  
  15.     </path>  
  16.       
  17.     <!-- Clear the directories of the out -->  
  18.     <target name='clear'>  
  19.         <echo>Delete the target directories</echo>  
  20.         <delete dir='bin' />  
  21.     </target>  
  22.       
  23.     <!-- =================================   
  24.           target: init                
  25.          ================================= -->  
  26.     <target name='init' depends='clear' description='Create the bin directory'>  
  27.         <echo>Create the bin directory</echo>  
  28.         <mkdir dir='${build}'/>  
  29.     </target>  
  30.       
  31.     <!-- =================================   
  32.               target: compile                 
  33.          ================================= -->  
  34.     <target name='compile' depends='init' description='Compile the project'>  
  35.         <javac srcdir='${src}'  
  36.          destdir='${build}'  
  37.          classpathref='classpath'  
  38.         />  
  39.         <copy todir='bin'>  
  40.             <fileset dir='src'>  
  41.                 <include name='**/*.properties'/>  
  42.             </fileset>  
  43.         </copy>  
  44.     </target>  
  45.       
  46.     <!-- =================================   
  47.           target: run                
  48.          ================================= -->  
  49.     <target name='run' depends='compile' description='run the project'>  
  50.         <java classpathref='classpath' fork='true' classname='<span style='color:#ff0000;'>video.info.Ku6</span>  
  51.   
  52. >  
  53.             <jvmarg value='-Xms400M' />  
  54.             <jvmarg value='-Xmx1024M' />  
  55.         </java>     
  56.     </target>  
  57. </project>  
<?xml version='1.0' encoding='UTF-8'?>
<project basedir='.' default='run' name='BHO Video indexing'>
<description>Buile the BHO video index</description>
<!--Set the Global variable-->
<property name='src'  location='src' />
<property name='build' location='bin'/>
<!-- =================================
指定程序运行需要的classpath引用,一般不用更改
================================= -->
<path id='classpath'>
<pathelement location='bin' />
<fileset dir='lib'>
<include name='**/*.jar' />
</fileset>
</path>

<!-- Clear the directories of the out -->
<target name='clear'>
<echo>Delete the target directories</echo>
<delete dir='bin' />
</target>

<!-- ================================= 
          target: init              
         ================================= -->
    <target name='init' depends='clear' description='Create the bin directory'>
        <echo>Create the bin directory</echo>
    <mkdir dir='${build}'/>
    </target>

<!-- ================================= 
          target: compile               
         ================================= -->
    <target name='compile' depends='init' description='Compile the project'>
        <javac srcdir='${src}'
         destdir='${build}'
         classpathref='classpath'
/>
    <copy todir='bin'>
    <fileset dir='src'>
    <include name='**/*.properties'/>
    </fileset>
    </copy>
    </target>

<!-- ================================= 
          target: run              
         ================================= -->
    <target name='run' depends='compile' description='run the project'>
     <java classpathref='classpath' fork='true' classname='video.info.Ku6

' >
     <jvmarg value='-Xms400M' />
     <jvmarg value='-Xmx1024M' />
     </java>   
    </target>
</project>

    b)在命令行(终端)上输入ant run程序就可以启动,其中run是target的名称,请注意修改上面代码中标红的部分,这个事你打算运行程序相对于bin目录的包结构。其中run中的参数-Xms -Xmx可以省略,如果你的程序运行不会超过java虚拟机默认分配的内存。如果系统不能运行ant命令,请修改响应的环境变量(ANT_HOME).

    2.直接控制台命令启动

    2.1 windows平台上启动java(手动安装jdk)

@echo off
set directory=E:\BenZhou\MIS\project\OpenJavaProg\  替换成你程序的根目录


set jdkpath=D:\Java\jdk1.6.0_07\ 替换成你jdk的安装目录


set classp=%jdkpath%lib\dt.jar;%jdkpath%lib\tools.jar;%directory%bin;%directory%lib\commons-codec-1.3.jar;%directory%lib\commons-httpclient-3.1.jar;%directory%lib\commons-logging-1.1.jar;%directory%lib\commons-net-2.0.jar;%directory%lib\jaxen-core.jar;%directory%lib\jaxen-jdom.jar;%directory%lib\jdom.jar;%directory%lib\log4j-1.2.15.jar;%directory%lib\saxpath.jar
后面的jar部分请替换成你自己引用的jar,前面dt.jar tools.jar bin都是必须得


%jdkpath%\bin\java -classpath %classp% open.mis.uploader.MainEntrance

    标红的是我给的标注,运行的时候请删除,将上面的存为bat批处理文件就可以直接运行了。

    2.2 Linux平台上命令行启动java(手动安装jdk)

java -classpath /usr/java/jdk1.6.0_05/lib/dt.jar:/usr/java/jdk1.6.0_05/lib/tools.jar:/home/benzhou/OpenJavaProg/bin open.mis.test.GetLocalIPAddress

    windows上分割包用的是;linux上用的是:这点是需要注意的。

    2.3 Linux平台上shell脚本启动

#!/bin/bash
#program:
#       This program is used to insert the info log to the database
#       The project is Controlled Short Video
#history
#2008/11/04     benzhou         First Release

#export the environment
JAVA_HOME=/usr/local/java/
export JAVA_HOME
export LANG=zh_CN.GB18030

#print the timestamp
echo `date` ' begin to build the white site'

#run the program
CLASSPATH=/program/bhocenter/bin:$JAVA_HOME/lib/rt.jar:$JAVA_HOME/lib/dt.jar
for jar in `ls /program/bhocenter/lib/*.jar`
do
        CLASSPATH='$CLASSPATH':'$jar'
done
/usr/local/java/bin/java -classpath $CLASSPATH center.database.DownloadWhiteSite
echo `date` ' process complete'

    上面的是通过脚本启动的,可以直接复制这个脚本将其中的主目录更改后即可,这个脚本会扫描lib目录,然后将所有的jar组织成命令启动需要的格式。

     2.4Windows上在脚本中使用for循环来读取lib中的内容,这个我还没有搞定,如果有搞定的朋友,麻烦告诉我,多谢

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约