2

I have the following file structure that I need to compile using the command line. I have been using Eclipse all this while and it works fine in the editor.

/src
     /maps
          Structures/ (.java files) using a 3rd party external JAR file in E:\Lib\math.jar
          Util/ (.java files)
          TestClasses/ (.java files) Test.java is the main class

I compiled the above files using the following javac directive

C:\src\maps>javac -classpath E:\Lib\math.jar Util\*.java Structures\*.java TestClasses\*.java

This compiles without a problem and creates the relevant .class files.

However, when I try to run the main class using the following java directive

C:\src\maps>java TestClasses\TestSOM

I get the following exception

Exception in thread "main" java.lang.NoClassDefFoundError: TestClasses\TestSOM (
wrong name: maps/TestClasses/TestSOM)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)

what is the correct way of executing the main class using the command line?

4
  • 3
    Why do you don't use Maven ? Commented Jan 15, 2014 at 13:21
  • @thiago.lenz yeah thinking about it now. But I still need a command line version since the target environment requires it. Commented Jan 15, 2014 at 13:28
  • 1
    @Synex Maven is command-line. Commented Jan 15, 2014 at 13:29
  • @chrylis unfortunately I don't have maven in the place I'm trying to run this program and I'm not allowed to Commented Jan 15, 2014 at 13:41

3 Answers 3

2

Use the fully qualified name of the class AND include the libraries as well to avoid the runtime errors for the classes referenced

C:\src\>java -classpath .;E:\Lib\math.jar map.TestClasses.Test

Assuming that Test.java has main method as you mentioned.

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

4 Comments

I get the following error when I do that Error: Could not find or load main class maps.TestClasses.TestSOM
The current directory should be included in the classpath as well, so that java can find your freshly compiled classes. e.g. java -classpath c:\src;E:\Lib\math.jar map.TestClasses.Test Consider compiling your sourcefiles into a separate directory. You can specify this with the -d switch from javac (see link )
Thank you @Andreas problem solved. But I do not know a way to give you credit for giving the correct answer since its a comment
@Andreas, thanks for pointing it out. I have corrected the answer.
0

use below:

C:\src>java maps.TestClasses.TestSOM

If your classes are inside the src dir. If any other change dir to that dir.

2 Comments

Do you have your compiled classes in your src dir?
As above your structure your main class is Test.java so you should use maps.TestClasses.Test
0
cd maps/TestClasses

java -classpath E:\Lib\math.jar;../Structures;../Util; Test

If your class is Test.java.

You should start off placing all of your classes in the same folder, will be easier to start your project. They need not be in separate folders, and when you do use separate folders, you want them to be in different "packages".

Comments

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.