PDA

View Full Version : Java Compile/Run Problem


Roost3r
03-21-2003, 01:39 AM
Hi,
I'm new to working with java; for my class lab assignment I had to copy files from one directory and put them in my /Java/Lab4/inventory/ directory.... i can compile my Inventory.java file by typing

javac -classpath $HOME/Java/Lab4 Inventory.java

that then creates Inventory.class file in that same directory... and i want to run it; thats where the problem is; to run it im typing

java -classpath $HOME/Java/Lab4 Inventory.class
I've also tried
java Inventory.class

The error I'm getting is

Exception in thread "main" java.lang.NoClassDefFoundError: /users/klepeis/Java/Lab4

any help would be greatly appreciated

Jason
03-21-2003, 01:57 AM
that says there is an error in the main of that file. At least thats how its read if it were C/C++ but you might try just Inventory.class, is there an object file to run otherwise?


Jason

Roost3r
03-21-2003, 02:02 AM
it compiles fine it just gives that error when i try to run it... there is no main in inventory; it is part of a package (called inventory)

... do you mean at the console try running it by typing just "Inventory.class" ?

the only files in the directory where my package is are .java and .class

Spookster
03-21-2003, 02:03 AM
You can use spaces in your file name:

Lab4 Inventory.java

if Lab4 is a directory then make sure you put a slash there. Also your filename must match the class name of your main method(to include capitalization. In other words if you are compiliing:

javac Inventory.java

then you should have a class defined in your file like so:

class Inventory{

public static void main(String[] bean){

}//end main

}//end Inventory

Spookster
03-21-2003, 02:04 AM
Originally posted by Roost3r
it compiles fine it just gives that error when i try to run it... there is no main in inventory; it is part of a package (called inventory)

... do you mean at the console try running it by typing just "Inventory.class" ?

the only files in the directory where my package is are .java and .class

oh ok. Then have your properly declared it as a package in your files?

Roost3r
03-21-2003, 02:18 AM
ive done it again compiling my file by typing:
javac -classpath $HOME/Java/Lab4/ InventoryTest.java
it creates InventoryTest.class
to run it i tried:
java -classpath $HOME/Java/Lab4/ InventoryTest.class and
java InventoryTest.class

error when trying to run:
Exception in thread "main" java.lang.NoClassDefFoundError: InventoryTest/class

why would it compile with no error then get a error on running...
I looked in the InventoryTest.java file and the main function looks fine...

since this is part of a package do i maybe need to do something liek this?
java InventoryTest.class Inventory.class ... (with all the classes of the inventory?)

Roost3r
03-21-2003, 02:19 AM
package inventory; is at the top of each package file and the directory it is in is called "inventory" (/Java/Lab4/inventory/)

files in the package are:
RevisedInventoryTest.java
RetailItemDB.java
InventoryTest.java
Inventory.java
DBTest.java

and 2 classes are used in some of these files
RetailItem.class
RetailItemList.class

Spookster
03-21-2003, 03:28 AM
In your main class if you are trying to import any of those files from the package you need to make sure those package files are already compiled. From your main directory compile those package file like so:

javac -classpath $HOME/Java/Lab4/inventory/RevisedInventoryTest.java

and do that for the rest of them. You might be able to do them all at once like:

javac -classpath $HOME/Java/Lab4/inventory/*.java

Then compile your main class file.

Roost3r
03-21-2003, 03:38 AM
ok ill try that , thanks alot

psp
03-21-2003, 06:47 AM
You don't have to give any extension to the file name when you are running it.
That is if you have a program called Inventory.java ,after u compile it , a class file is generated Inventory.class.
When u run it, just the command would be
java Inventory
NOT java Inventory.class
No extension is required.(.class)
See if this was the problem.