1

I have the following directory structure:

folder1/
    folder2/
        compiler.java

For a school assignment, we have to be able to run the following commands from folder1:

javac folder2/compiler.java
java  folder2/compiler

The compilation with javac works. When I try to run it with the above java command, I get a

Exception in thread "main" java.lang.NoClassDefFoundError: folder2/compiler (wrong name: compiler)
3
  • javac should be emiting the .class files to the same folder (unless yours is configured differently). You should be able to just java compiler in the same directory that the compiler.class file is in. Commented Jan 29, 2012 at 21:15
  • Are you sure you are not running java folder2.compiler.java instead of java folder2.compiler? Commented Jan 29, 2012 at 21:20
  • Banthar: I was running java folder2/compiler.java. When I run java folder2/compiler, I still get an error message (see edited error in question) Commented Jan 29, 2012 at 21:23

2 Answers 2

2

You have two options.

If your class compiler is in the default package do this. (No package declaration)

javac folder2/compiler.java
java -cp folder2/ compiler

Otherwise, if your class compiler is in the package folder2 then do this.

javac folder2/compiler.java
java -cp . folder2.compiler

You can put complier in the folder2 package by putting a package declaration at the top of compiler.java

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

2 Comments

Is there no way to be able to run javac folder2/compiler.java then java folder2/compiler? I have to get those to commands working exactly for a grading script...
@gonzoc0ding You may omit the -cp argument if you set the CLASSPATH in the environment. So using my first example, after the javac you can do export CLASSPATH="folder2/" then java compiler. This is assuming a unix-like sh or bash is what you are working from.
0

Try

java -cp . folder2.compiler

make sure class compiler is in the folder2 package

Comments

Your Answer

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