Where I work we have a shell script that allow us to execute arbitrary Java classes with all the necessary libraries and settings, something like:
#!/bin/sh
$JAVA_HOME/bin/java -cp LONG_LIST_OF_JARS -Xmx6g -XX:MaxPermSize=128m "$@"
And used like so:
javacorp.sh com.mycorp.SomeJob
This is obviously better than needing to specify all the java
arguments explicitly every time, but I dislike that it's only manually connected to the jars configured in our Eclipse project for compiling the codebase. I'm working on a personal project and looking to similarly being able to execute arbitrary classes from the command line, and trying to identify how to best provide a consistent java
environment.
Currently I'm using Eclipse to run my applications, but I'd like to be able to run them from the command line directly, or on machines that don't have Eclipse installed. In particular, I'd also like to be able to limit the scope of classes/jars that can be executed. For example, javacorp.sh
lets us run anything 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 at the command line, with minimal boilerplate? - Is there any way to hook into the
.classpath
file Eclipse creates? This doesn't wholly solve my problem (e.g. consistent memory settings) but it'd be nice to use data that already exists.
Edit:
Another way of phrasing my question would be "What's the cleanest way to replicate Eclipse's easy 'Run the current file's main method' button on the command line?"