25

I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:

java.lang.NoClassDefFoundError: commandprogram/CommandProgram

This is my script:

#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram

Changing the java line to the following:

java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram

produces the same results.

4
  • could you post your script, that will give people a better idea on where to look at... Commented Jul 27, 2009 at 19:15
  • Don't you need to do java commandprogram/CommandProgram.class or java -jar commandprogram/CommandProgram.jar? Commented Jul 27, 2009 at 19:29
  • isnt' the path separator a colon (:) instead of the semicolon (;) you've used? Commented Jul 28, 2009 at 3:21
  • The path separator is a colon on *nix, semicolon on Windows. I'm not sure which one you use in cygwin. Commented Jul 29, 2009 at 21:09

4 Answers 4

33

add your directory to classpath example:

java -classpath commandprogram CommandProgram

or

java -classpath directory_to_program Program
Sign up to request clarification or add additional context in comments.

Comments

5

After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:

#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram

Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."

I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.

Comments

1

you have to use a dot to separate packages, not a slash.

java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram

1 Comment

/ works too. Keep in mind that the script works when I'm in the directory with the scripts.
-10

The usual way of running a java file is to save it in the Java/Bin folder and Run cmd

C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname

If you save the file in different directory such as D:, you can use the following on the cmd prompt:

D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

1 Comment

Saving your java files or class files in the Java bin directory is, in fact, very unusual and should be avoided. The Java bin directory is for Java's own binary files and nothing else.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.