Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run java program from cmd line with classpath and native libraries?

Ok I give up - I dont have a lot of xp running java from cmd line and I've been at this for a while, tried endless combinations and I am just frustrated so here we go.

I need to run a simple java app (with dependencies on a bunch of jars that then depend on some native libraries) from cmd line - I am in the bin folder when I try to run the following:

java -Djava.library.path="../lib" -cp ../jar;. MyMainClass

No matter the order (i understand the order counts!) this does not work, with different errors.

For example the version I have pasted above gives me a list of the cmd line params followed by:

-bash: myMainClass: No such file or directory

but MyMainClass.class is there in the bin folder from which I am running the stuff.

If I try this instead:

java -Djava.library.path="../lib" AlphaHHKernel_Tuning_Test -cp ../jar;.

I get:

Exception in thread "main" java.lang.NoClassDefFoundError: MyMainClass
Caused by: java.lang.ClassNotFoundException: MyMainClass
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
-bash: .: filename argument required
.: usage: . filename [arguments]

I'd like to understand how those attributes are supposed to be used (and get it to work in the process).

like image 617
JohnIdol Avatar asked Apr 14 '26 10:04

JohnIdol


1 Answers

in Unix specify classpath with a : delimiter, not ; as in Windows: -cp ../jar:.

Also, you need to specify each jarfile, not just give it a directory full of jars.

like image 199
jcomeau_ictx Avatar answered Apr 16 '26 22:04

jcomeau_ictx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!