Running program in command prompt
I have two files: InventoryMain.java and Inventory.java. I am able to compile both with no errors and run the program in netbeans with no problem; however, I am able to compile both using command prompt, so now I have .class files for both, but when I try to execute the program while in command prompt with java InventoryMain I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: InventoryMain (wrong name: inventory/InventoryMain)
This is the first program I have created that has two different files to use, is there a different syntax to use in command prompt other then just java InventoryMain to get program to execute since there is two .class files? Like I said, this program compiles and runs perfectly when I run it using netbeans.
Any help would be greatly appreciated. Thank you!!
Sounds like you need to use the -classpath option to java
Read more on this here:
http://en.wikipedia.org/wiki/Classpath_(Java)
Another option is to create an executable jar file for your application that contains all of your classes.
Read more on this here:
http://java.sun.com/docs/books/tutorial/deployment/jar/index.html
From that Exception I think the problem is that the JVM cannot find your main method class InventoryMain which looks like it is in package inventory.
When running a Java app from the command prompt important things to remember are current location (present working directory) and fully qualified class name of the main class (package.Class).
For example:
package inventory;
public class InventoryMain {
public static void main(String[] args) {
InventoryMain im = new InventoryMain();
im.execute;
}
private void execute() {
System.out.printlln("It worked!");
}
}
location on hard-drive = c:\mycode\testing\inventory\InventoryMain.class
1) Bad
Current Directory = c:\mycode\testing\inventory
c:\mycode\testing\inventory> java InventoryMain
Exception in thread "main" java.lang.NoClassDefFoundError: InventoryMain (wrong name: inventory/InventoryMain)
at java.lang.......
at ......
2) Bad
Current Directory = c:\mycode\testing\inventory
c:\mycode\testing\inventory> java inventory.InventoryMain
Exception in thread "main" java.lang.NoClassDefFoundError: inventory/InventoryMain
3) Bad
Current Directory = c:\mycode\testing
c:\mycode\testing> java InventoryMain
Exception in thread "main" java.lang.NoClassDefFoundError: InventoryMain
4) Good
Current Directory = c:\mycode\testing
c:\mycode\testing> java inventory.InventoryMain
It worked!
Hope that helps to clear it up a bit?
Rich