0

My source files are in this folder: c:\data\mycompany. All of my source files contain the following as the first line: package mycompany; Now from the c:\data folder, I compiled everything using this command: javac mycompany/*.java -extdirs c:\some\other\folder\with\libs. This compiles fine. Now when I try to execute it (again from c:\data) using this command: java mycompany/test then i get this error:

Exception in thread "main"
java.lang.NoClassDefFoundError:
mycompany/test Caused by:
java.lang.ClassNotFoundException:
mycompany.test at java.net.URLClassLoader$1.run(Unknown Source)

I also tried the below command but it reproduces the same error:

java mycompany/test -extdirs c:\some\other\folder\with\libs

Is this the proper way to compile/run?

Here is my source-code:

package MyCompany;

public class Test
{
  public static void main(String[] args) 
  {
    System.out.println("test");
  } 
}
vikasde
  • 5,681
  • 10
  • 45
  • 62
  • you have to include every subpath that has classes in it not just the top level one and you need to fully qualify the package name to the main class mycompany.Test, class names should always be uppercase –  Feb 22 '10 at 20:27
  • your package name should be the exact same case your the directory name which according to your example is _mycompany_ case is important –  Feb 22 '10 at 20:44
  • I also changed my folder to be "MyCompany". I am still getting the same error :( – vikasde Feb 22 '10 at 20:47

4 Answers4

2

You need to set the classpath. See for example this SO question.

Community
  • 1
  • 1
Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
2

You shouldn't be using extdirs when invoking java, you should be setting your classpath with -cp

By the way, when invoking a main java class, you should provide the class name, not the path to the class, hence, it's likely mycompany.test (if your class that contains main is named test), not mycompany/test. It's not an error as Java fixes it for you.

Uri
  • 88,451
  • 51
  • 221
  • 321
1

that is saying that the .class files are not on the classpath how you are compiling should be fine, you need to add the directory with the resulting .class files to your classpath when you try and run your test code.

java -cp <path to classes> classtorun

so your example might look like

java -cp <path to classes>;<path to libs> mycompany.Test

you should really look at ANT to automate your compile and build to an executable .jar file. Nobody does this fiddly stuff by hand because of all the repetitive typing and chances for errors. ANT was created to avoid all this minutia and let you concentrate on solving coding problems and not struggling with the command line tools. Read this.

  • Should be running this from c:\data or from c:\data\mycompany ? – vikasde Feb 22 '10 at 20:26
  • I will look at ANT, once I get this running at all :) – vikasde Feb 22 '10 at 20:29
  • you should run it from the root of the project which is the default package right before you start declaring packages. You can run it from whereever as long as you fully qualified paths –  Feb 22 '10 at 20:30
  • 1
    ANT is FOR getting this "running at all" __NOBODY__ does this kind of stuff from the command line by hand. ANT is there to automate all the fiddly bits you are struggling with. –  Feb 22 '10 at 20:31
  • @fuzzy Nothing seems to work here. I tried it from c:\data and from c:\data\mycompany. My command is: java -cp C:\data\mycompany mycompany.test – vikasde Feb 22 '10 at 20:32
  • do you have a proper package statement at the top of mycompany.test and test should actually be Test and in a file called Test.java and have a proper package statement at the top. –  Feb 22 '10 at 20:35
  • yes, I do have the package statement at the top. I will change it to uppercase, but that shouldnt change anything. – vikasde Feb 22 '10 at 20:36
  • your package statement is wrong it should be the exact same case as the directory name –  Feb 22 '10 at 20:44
  • I renamed mycompany to MyCompany. Is that what you are refering too or should I also include **Data**? – vikasde Feb 22 '10 at 20:45
  • your package statement is wrong it should be the exact same case as the directory name –  Feb 22 '10 at 20:47
  • Yes. I renamed the folder as well to be upper case and the file as well. I recompiled and tried to run the cmd again, but I am still NoClassDefFoundError. – vikasde Feb 22 '10 at 20:53
  • Regarding your link: I can run the class fine, if I do not use any packages. The packages is what causing the issue for me here. – vikasde Feb 22 '10 at 20:55
  • @Fuzzy: This helped me finally: http://www.jarticles.com/package/package_eng.html - Thanks for the help though. I appreciate it. – vikasde Feb 22 '10 at 20:59
0

Try this to compile:

mkdir classes
javac -d classes *.java

Only create the /classes directory the first time. The -d directory tells javac to put your .class file under /classes.

To run, do this:

java -cp .;classes MyCompany.Test

This should work fine.

duffymo
  • 305,152
  • 44
  • 369
  • 561