Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating packages... 2

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
If I insert into my code (mycode.java):

package myfirm.foo.project

I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: mycode/myfirm.

Wich is the way to create packages????
 
Ok, so you create a file MyCode.java:

package myfirm.foo.project;

public class MyCode {
...
}

First of all, this file needs to be placed in a folder called project, which is in a folder called foo, which is in a folder called myfirm.

when you try to execute this class you need to have the root directory (myfirm) in the class path and you would say:

java myfirm.foo.project.MyCode

to execute it.

Hope this helps. Packages were very confusing for me at first.

Charles
 
I inserted 'package myfirm.foo.project' into 'mycode.java'

I created 'myfirm' folder; 'foo' folder; 'project' folder, and placed 'mycode.java' on it.

I added __/myfirm to my CLASSPATH

But when execute 'java myfirm.foo.project.mycode', I get the same error...



 
Now, the error is:

Exception in thread "main" java.lang.NoClassDefFoundError: myfirm/foo/project/mycode
 
Nowhere do you say that you actually compiled the file. Placing mycode.java in the folder is insufficient. You need to compile it so that mycode.class is in the myfirm/foo/project folder.
 
Yes. I do compile mycode.java, and I do place mycode.class
into 'myfirm/foo/project'.

But if I execute 'java myfirm.foo.project.mycode', I get:

Exception in thread "main" java.lang.NoClassDefFoundError: myfirm/foo/project/mycode
 
Sounds like a CLASSPATH issue. What directory are you running the java command from? Regardless, make sure you add the myfirm directory to your CLASSPATH. Also add . (current directory) to your CLASSPATH if it isn't already.
Wushutwist
 
don't add the myfirm folder to your class path, you have to add the folder above it i.e. if your code is looking for myfirm and it is in the folder

c:\myproject\src\classfiles\myfirm

you have to add the following to your classpath

c:\myproject\src\classfiles

the jvm will look under this folder for the first entry in your package name i.e. myfirm. so, as you can see, if you add

c:\myproject\src\classfiles\myfirm

to your classpath, it wont find myfirm because its already there...see!!! X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top