0

When I run the following in Windows it works correctly:

java -cp bin\;lib/myJar.jar com.example.MyClass

But I cannot get it to work in Ubuntu (it says Could not find or load main class). I read a couple of posts in SO, and the problem I think is in the way the class path needs to be set, but I cannot figure out how I can modify bin\;lib/myJar.jar to run correctly on ubuntu.

FranXh
  • 4,481
  • 20
  • 59
  • 78
  • At this point the incompleteness of your original question is not only very clear but also a breaking point. That's two cases of "already tried it" that you did not care to document and you also have not shared yet how you know that "it doesn't work". You must be getting an error, post it so we don't have to grasp for straws in the dark. – Gimby Aug 19 '14 at 14:44
  • I tried various combinations of :, \:, :. (while reading the official Java documentation and posts on SO), and I though it is not very smart to post all the combinations that I tried. In addition the problem is always "Could not find or load main class) – FranXh Aug 19 '14 at 14:45
  • It IS smart to be complete. – Gimby Aug 19 '14 at 14:48
  • First of all, why are you providing two locations, which of them ought to contain the class file, next, did you verify that the class file is at the expected location *and* has the right name regarding upper vs. lower case? Did you check the output of `jar tf lib/myJar.jar` and `ls -R bin`? – Holger Aug 19 '14 at 14:59
  • Oh right, forgot to ask this yesterday... have you considered making it an [executable jar](http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file)? – Powerlord Aug 20 '14 at 13:52
  • No, that is actually a good idea – FranXh Aug 21 '14 at 14:00

2 Answers2

2

You are executing your cmd in UNIX system , you should follow convention of unix :

don't use \ as directory separator, use / instead. don't use ; as classpath separator , use : instead.

so you have to move to the directory where your myJar.jar is located, if the main class that you want to execute is supposed to be in the jar ,then check that it's really there using the command :

jar tf jar-file

that will print the content of your jar.if you have a folder bin in the same folder you can add it to the classpath

then use

java -cp "bin/:lib/myJar.jar" com.example.MyClass
Mifmif
  • 3,132
  • 18
  • 23
1

UNIX-based operating systems don't recognize backslashes in directory paths.

Try using bin/ instead.

On a side note, Windows will recognize either \ or / as a directory separator.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 1
    The semicolon is also to be replaced with a colon. – Gimby Aug 19 '14 at 14:39
  • @Gimby Good point, I didn't realize Oracle had separate documentation files for Classpath based on the OS ([Windows](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html), [UNIX](http://docs.oracle.com/javase/8/docs/technotes/tools/unix/classpath.html)). Although I guess it does make sense that Classpath would follow the Path separator rules for the OS. – Powerlord Aug 19 '14 at 14:42