How to automatically create batch / shell scripts to run a Java console application?

瘦欲@ 提交于 2019-11-29 02:01:12
khmarbaise

In Maven the best solution for this is the maven-appassembler-plugin which handles the creation of a shell script / batch file. In combination with maven-assembly you can create a tar.gz or zip archive which contains everything which is needed.

Maven knows the dependency:build-classpath goal, which does most of the dirty work:

mvn dependency:build-classpath -DoutputFile=cp.txt

You can use this generated file in a shell script to create the java classpath (I know, it ain't much, but it'll get you started).

Or you can use the exec-maven-plugin to launch a main class from the current maven context. Something like this will do:

mvn compile org.codehaus.mojo:exec-maven-plugin:1.2:java \
    -Dexec.mainClass=com.yourcompany.YourClass

One approach is to use a binary executable builder for Java applications. Some of these can be launched from ant with provided ant tasks.

For example, exe4j can create executables for command-line applications, and supports Windows, Linux and Mac. The executable wraps the Java code, its classpath, and JVM search path. A custom ant task "com.exe4j.Exe4JTask" supports generation of the executable from an exe4J configuration -- which can be created with a friendly wizard.

Apache commons Launcher creates startup scripts for you. Both Windows and Linux script will be generated. Also it can be integrated with Ant.

I hope it will help others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!