12

Ok I know this question has been asked many many many times before, but I've googled it and looked at examples and looked at questions on SO for the past month, and I seriously cannot get this to work. I think the problem is that I want to be able to run the program from both Eclipse and the command line. I'm also using OSX and I think a lot of the examples I'm reading are for Windows/Linux.

If I have a simple program compiled in Eclipse that I want to run from the command line I do this:

java -cp bin MyProgram

I have another program I compile and run in Eclipse, and this references the MySQL JDBC connector (mysql-connector-java-5.1.19-bin.jar) which is stored in the same directory. This works fine from Eclipse, but I can't run it from the command line.

I've tried all combinations of things...

java -classpath "bin;mysql-connector-java-5.1.19-bin.jar" MyProgram
java -cp bin\;mysql-connector-java-5.1.19-bin.jar MyProgram

and get all sorts of class not found errors...

Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram
Caused by: java.lang.ClassNotFoundException: MyProgram
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Matt
  • 11,157
  • 26
  • 81
  • 110
  • Hi Matt, if i'm not mistaken I think the classpath is to declare the path to the main class you want to execute. So in this case if your class is in the bin folder I would just try: java MyProgram or java -cp myprogrampath MyProgram – Federico Giust Apr 30 '12 at 14:18
  • 1
    @FedericoGiust Not specifically; classpath just defines an archive or a path to a set of classes. The classpath may or may not include the runnable class (which usually results in an exception or straight up java.exe error). Classpath can include directories and archives that don't have runnable classes. – Qix - MONICA WAS MISTREATED Dec 31 '12 at 01:28
  • possible duplicate of [Java command line with external .jar](http://stackoverflow.com/questions/6069702/java-command-line-with-external-jar) – J-16 SDiZ Dec 31 '12 at 07:22

5 Answers5

14

Your problem is min separator you are using. Separator ; is for windows. On Unix systems you should use : instead:

java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram

AlexR
  • 114,158
  • 16
  • 130
  • 208
5

Use a ':' to separate your entries on Unix systems:

java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram
java -cp bin:mysql-connector-java-5.1.19-bin.jar MyProgram

Eclipse converts it automatically.

mprivat
  • 21,582
  • 4
  • 54
  • 64
1

See:

String pathSeparator = System.getProperty("path.separator");
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

you did not set your main class in classpaht, try add ./ in -cp

user1335794
  • 1,082
  • 6
  • 12
  • I doubt that the current directory contains its main class, I would believe 'bin' contains those. – Guillaume Polet Apr 30 '12 at 14:17
  • the `MyProgram` has to be set into classpath, also you need add pachage name if you put `MyProgram` into package. – user1335794 Apr 30 '12 at 14:20
  • Don't worry, I know all about classpath and fully qualified name. I am just saying that adding the current directory to the classpath is useless if his classes are located in the bin directory. – Guillaume Polet Apr 30 '12 at 14:23
  • @GuillaumePolet in this case what is the solution if I dont want to specify jar name in front of -cp option. is there any solution i can define current directory in classpath and all of its jars come in classpath. – Vipin Jan 21 '14 at 11:00
  • @Vipin you can always use the wildcards stuffs (as of Java6): -cp .;./*.jar or on Unix/Linux .:./\*.jar (Beware of Shell expansion) – Guillaume Polet Jan 21 '14 at 14:41
-2

I would highly suggest you try --jar or -jar. I can't remember which it is, but those should settle you. Also, if you have the dev tools from apple, they have a jar packager.

Johm Don
  • 609
  • 2
  • 5
  • 15
  • 1
    It's -jar, but obvisouly his classes are not jared up. Forget about Apple dev tools they are from the previous century. Eclipse, NetBEans and IntelliJ are light years ahead of those. – Guillaume Polet Apr 30 '12 at 14:18
  • @GuillamePolet Agrred, but I only mentioned them do to them coming with a utility for producing apps from jars – Johm Don Apr 30 '12 at 14:19