Running Java programs in a consistent environment

Where I work, we have a shell script that allows us to execute arbitrary Java classes with all the necessary libraries and settings, for example:

#!/bin/sh $JAVA_HOME/bin/java -cp LONG_LIST_OF_JARS -Xmx6g -XX:MaxPermSize=128m " $@ " 

And used like this:

 javacorp.sh com.mycorp.SomeJob 

This is clearly better than having to explicitly specify all java arguments every time, but I donโ€™t like that it is only manually connected to banks configured in our Eclipse project to compile the code base. I am working on a personal project and seems to be able to execute arbitrary classes from the command line and try to determine how best to provide a consistent java environment.

I am currently using Eclipse to run my applications, but I would like to be able to run them from the command line directly or on machines that do not have Eclipse. In particular, I would also like to limit the amount of classes / jars that can be performed. For example, javacorp.sh allows us to run something in our src/ directory, and only javacorpunit.sh includes classes in the tests/unit/ directory in the classpath.

  • Is there a clean way to use Ant, Maven, or some other build tool to execute a configured java command on the command line with a minimal template?
  • Is there a way to connect to the .classpath file .classpath by Eclipse? This does not completely solve my problem (for example, consistent memory settings), but it would be nice to use existing data.

Edit:

Another way to formulate my question is: "What is the cleanest way to repeat Eclipse easy" Run the current main file method "on the command line?"

+3
java command-line-interface shell build ant
source share
3 answers

Have you considered shell scripting from Ant? The <pathconvert> task can create a class path from <fileset> .

You can create an Ant target for each type of shell script you want. make can then invoke these Ant targets to generate shell scripts.

+2
source share

I think the solution to your problem is to create an executable jar. Something that can be run as follows:

 java -jar myapp.jar 

"look ma, no classpath" :-)

The secret is to add the "Main-Class" and "Class-Path" entries to the jar manifest file. This tells java what to run and which banks should be loaded into the classpath:

 <jar destfile="${jar.file}" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${jar.main.class}" /> <attribute name="Class-Path" value="${jar.classpath}" /> </manifest> </jar> 

To help create the classpath, ANT has a really useful manifestclasspath task:

 <manifestclasspath property="jar.classpath" jarfile="${jar.file}"> <classpath> <fileset dir="${dist.dir}/lib" includes="*.jar"/> </classpath> </manifestclasspath> 

Thus, using this example, at runtime, java expects the parameters to be in the lib subdirectory (the task will generate relative links).

Eclipse integration is complex. The best approach for managing a class of classes is to use a dependency manager like ivy , which has an Eclipse plugin. Thus, both ANT and Eclipse use the same mechanism to manage build dependencies.

Finally, for a complete working example, take a look at:

  • Class not found with Ant, Ivy and JUnit - error in build.xml file?

Hope this helps.

+1
source share

With the Bazel system , you can easily determine as many running Java programs as you want with java_binary . You can define as many java_binary targets as possible, and all of them can depend on the same set (or overlapping sets) of java_library targets, so you don't need to manually check the classpath of each binary file.

In addition, for the purpose of java_binary you can also generate a *_deploy.jar file, which is a stand-alone executable Jar that you can run anywhere.

0
source share

All Articles