3

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).

Charles
  • 48,924
  • 13
  • 96
  • 136
JohnIdol
  • 45,251
  • 58
  • 153
  • 235
  • possible duplicate of [Setting multiple jars in java classpath](http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath) – Jahan Nov 25 '14 at 11:12

1 Answers1

2

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.

jcomeau_ictx
  • 35,098
  • 6
  • 89
  • 99
  • mmm ... now at least I get the same error with both versions if using: -cp ../jar/:. but not sure what that means – JohnIdol Jun 20 '11 at 01:08
  • see latest edit, it took me awhile to register the more blatant omission! – jcomeau_ictx Jun 20 '11 at 01:09
  • thanks man, you rock - that worked (but only with -cp before the main class). It's kind of weird that you cant point to the folder ... but that's what I get for not reading 'the instructions' I guess – JohnIdol Jun 20 '11 at 01:15
  • generally in Unix the options come before the arguments, and the main class is the argument in this case. – jcomeau_ictx Jun 20 '11 at 01:16
  • yeah that's fine - but I am still weirded out by the fact that I can't just point to the folder as *.jar or smt – JohnIdol Jun 20 '11 at 01:19
  • 3
    you could with some trickery, something like: -cp $(echo ../*.jar | tr ' ' ':'):. because the shell expands wildcard matches with spaces, and Java wants its classpath with colons – jcomeau_ictx Jun 20 '11 at 01:23
  • Strictly speaking it is not Java that wants its CLASSPATH with colons. A typical unix shell commandline looks like this _> program -option -complicatedoption=doit argument_ however you can write more than one "command" on one line by seperating them with a semicolon, just as you can write more than one Java statement on one line by seperating them with a semicolon! Hence the original attempt of JohnIdol did not work and got the strange bash error message because it was interpreted as 2 commands! First one: "java -Djava.library.path="../lib" -cp ../jar" and then the second one: ". MyMainClass" – Angel O'Sphere Aug 19 '11 at 14:28