views:

146

answers:

4

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");
  } 
}
+1  A: 

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.

fuzzy lollipop
Should be running this from c:\data or from c:\data\mycompany ?
vikasde
I will look at ANT, once I get this running at all :)
vikasde
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
fuzzy lollipop
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.
fuzzy lollipop
@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
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.
fuzzy lollipop
yes, I do have the package statement at the top. I will change it to uppercase, but that shouldnt change anything.
vikasde
post your source code in your question
fuzzy lollipop
your package statement is wrong it should be the exact same case as the directory name
fuzzy lollipop
I renamed mycompany to MyCompany. Is that what you are refering too or should I also include **Data**?
vikasde
your package statement is wrong it should be the exact same case as the directory name
fuzzy lollipop
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
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
@Fuzzy: This helped me finally: http://www.jarticles.com/package/package_eng.html - Thanks for the help though. I appreciate it.
vikasde
+1  A: 

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

Esko Luontola
+2  A: 

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
A: 

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