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)
3
  • 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 Commented Apr 30, 2012 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. Commented Dec 31, 2012 at 1:28
  • possible duplicate of Java command line with external .jar Commented Dec 31, 2012 at 7:22

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Any reason for escaping the colon?
1

See:

String pathSeparator = System.getProperty("path.separator");

Comments

0

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

5 Comments

I doubt that the current directory contains its main class, I would believe 'bin' contains those.
the MyProgram has to be set into classpath, also you need add pachage name if you put MyProgram into package.
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.
@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 you can always use the wildcards stuffs (as of Java6): -cp .;./*.jar or on Unix/Linux .:./*.jar (Beware of Shell expansion)
-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.

2 Comments

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.
@GuillamePolet Agrred, but I only mentioned them do to them coming with a utility for producing apps from jars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.