Reputation: 3993
Is there a way to tell jvm to look for a class with entry main
method automatically rather than to specify it when invoking java ClassWithMainMethod
?
In other words, I have directory with many compiled classes one of which has main
method and I want to only specify that directory.
Upvotes: 2
Views: 3276
Reputation: 39990
If, in your scenario:
javap
command is available – i.e. you've got a JDK installed, not just a JRE;you could use the following bash script:
#!/usr/bin/env sh
for classfile in *.class; do
classname=$(echo $classfile | cut -f 1 -d '.')
echo $classname
if javap -public $classname | fgrep 'public static void main(java.lang.String[])'; then
java $classname "$@"
fi
done
Upvotes: 7
Reputation: 2187
If there are more then one class with main method?
I think you can create a executable Jar file and specify class having main function in manifest file. So, whenever user will run jar file. Class with main function will be executed.
Upvotes: 0
Reputation: 236170
If your application is inside a .jar file, you can specify the main class in its manifest
Upvotes: 0
Reputation: 692231
No, there is no way to do that. You can create an executable jar, and define what the main class is in the jar manifest, allowing you to use
java -jar MyJar.jar
though.
See http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
You may also deliver your app with a script which executes the appropriate command for you:
#startup.cmd
java -cp ... com.foo.bar.Main
Upvotes: 5