執行java程序是不是隻能java -jar?

java -jar xxx.jar params 的形式用了這麼多年,都沒在意原來java的執行命令可以多元的。

這兩天剛好在看rocketmq,順便看了下它的shell腳本,然後才發現原來我這麼多年的java -jar有多麼的無知。


先來一段命令:

java -Djava.ext.dirs=$ROCKETMQ_HOME/lib -cp  $ROCKETMQ_HOME/lib/rocketmq-example-3.5.8.jar com.alibaba.rocketmq.example.simple.Producer


這段命令是爲了測試rocketmq的消息產生類產生消息。


於是回頭再看一下java -help,瞬間知道了。那我們許多的工程寫法,其實都不用糾結java打包的啓動類是否在打包時候已經打入,也不用糾結一個jar包有N多個入口。


2012年時候我在某個項目做一個二級中心的時候,那時候爲了一個工程中的三個入口都被調用到,同一套代碼,配置了三個META-INF,裏面配置了入口類,同時打了三個jar,如果改動代碼再打包,費時費力。

現在回想,如果現在來做,就是打一個jar包,編三個shell腳本,瞬間解決一切。那些創造這些事務的人真是天才,使用的人可能就需要無盡的學習。


[rocketmq@cent65-1 ~]$ java -help

Usage: java [-options] class [args...]

           (to execute a class)

   or  java [-options] -jar jarfile [args...]

           (to execute a jar file)

where options include:

    -d32          use a 32-bit data model if available

    -d64          use a 64-bit data model if available

    -server       to select the "server" VM

                  The default VM is server,

                  because you are running on a server-class machine.



    -cp <class search path of directories and zip/jar files>

    -classpath <class search path of directories and zip/jar files>

                  A : separated list of directories, JAR archives,

                  and ZIP archives to search for class files.

    -D<name>=<value>

                  set a system property

    -verbose:[class|gc|jni]

                  enable verbose output

    -version      print product version and exit

    -version:<value>

                  Warning: this feature is deprecated and will be removed

                  in a future release.

                  require the specified version to run

    -showversion  print product version and continue

    -jre-restrict-search | -no-jre-restrict-search

                  Warning: this feature is deprecated and will be removed

                  in a future release.

                  include/exclude user private JREs in the version search

    -? -help      print this help message

    -X            print help on non-standard options

    -ea[:<packagename>...|:<classname>]

    -enableassertions[:<packagename>...|:<classname>]

                  enable assertions with specified granularity

    -da[:<packagename>...|:<classname>]

    -disableassertions[:<packagename>...|:<classname>]

                  disable assertions with specified granularity

    -esa | -enablesystemassertions

                  enable system assertions

    -dsa | -disablesystemassertions

                  disable system assertions

    -agentlib:<libname>[=<options>]

                  load native agent library <libname>, e.g. -agentlib:hprof

                  see also, -agentlib:jdwp=help and -agentlib:hprof=help

    -agentpath:<pathname>[=<options>]

                  load native agent library by full pathname

    -javaagent:<jarpath>[=<options>]

                  load Java programming language agent, see java.lang.instrument

    -splash:<imagepath>

                  show splash screen with specified image

See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章