0

When I run

javac 'path/to/test.java'
java 'path/to/test'

I get an error like this

Exception in thread "main" java.lang.NoClassDefFoundError: path/to/test (wrong name: test)

It works when I do the same after I run "cd path/to", but is there a way to do this without the cd command? If so, how?

4
  • The path/to/Test.java needs to be relative to a sourcepath. The java command doesn't take a path, it takes a fully-qualified class name - periods, not slashes. It is relative to a classpath. (And the class name should start with an upper-case letter.) docs.oracle.com/javase/8/docs/technotes/tools/windows/… docs.oracle.com/javase/8/docs/technotes/tools/unix/… Commented Aug 12, 2016 at 22:55
  • @bphilipnyc, There's nothing wrong with learning how the command-line tools work. An IDE is great for editing, and for manually running unit tests, but if your checkins are built and tested and packaged by a continuous integration system, then sooner or later you're going to need to understand the command-line tools. Commented Aug 13, 2016 at 0:04
  • I haven't run javac in like 10 years. No harm in it of course, but you're better off spending that time learning true programming principles. Commented Aug 13, 2016 at 0:07
  • It will also identify some common syntax errors that newer programmers commonly make... OP could be missing a package statement, for example, that could have been caught by just about any IDE (see @stackoverflowuser2010's answer). Commented Aug 13, 2016 at 0:18

3 Answers 3

1

If you wish to compile and run the class from your current location (presumably a project directory), I would recommend:

  1. Use a package declaration in your class:

    package path.to;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println("LOADED");
        }
    }
    
  2. When you run your code do so with dot notation to the class name:

    javac path/to/Test.java
    java path.to.Test
    
Sign up to request clarification or add additional context in comments.

Comments

1

Your Test.java file should have a package line at the top:

package path.to;

public class Test {

    public static void main(String argv[]) {
    }
}

Then you can compile and run it. It works on my MacBook Pro. Note that java path.to.Test and java path/to/Test are equivalent.

% javac path/to/Test.java
% java path.to.Test

If you are missing the package statement, then you will get the error that you are reporting.

% javac path/to/Test.java
% java path.to.Test
Exception in thread "main" java.lang.NoClassDefFoundError: path/to/Test (wrong name: Test)

2 Comments

Shouldn't that be java path.to.Test?
@Andreas: java path.to.Test and java path/to/Test are equivalent in this context. I'll modify the answer to clarify.
0

Assuming test.java does not have a package statement, your class (which should be named Test, not test) is in the unnamed package, so do this:

javac 'path/to/test.java'
java -cp 'path/to' test

If your test.java file starts with a package to; statement, then you do this:

java -cp 'path' to.test

And if it says package path.to;, then you do this:

java path.to.test

or this to be explicit:

java -cp . path.to.test

If your package statement says any other name, then your directory structure is wrong, since the directory must match the package statement, starting at the directory named in the classpath (-cp).

2 Comments

Thanks! But what if I did have a package statement?
@stacko That depends on what the package statement says.

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.