• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't use package. Exception in thread "main" java.lang.NoClassDefFoundError.  RSS feed

 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem in running the program while using two or more classes in the same package.

Class1: OnlyCla.java



Class2: PackageTest.java



I compile these file by using following command:
javac -d . OnlyCla.java
javac -d . PackageTest.java


And set the classpath as:


Run the file:
java PackageTest

Now it is throwing the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: PackageTest (wrong name: p1\PackageTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)


This program is working on NetBeans. If I remove the "package" line from the code in the both files and run the program normally it is giving the same error.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is solved. I just ran like:
java p1.PackageTest

Wondering why was the previous method not working? And It wasn't working, even if I went to directory p1 and use the command java PackageTest
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the name of the class is p1.PackageTest, not PackageTest. The error message even tells you what the right name is. The packages are supposed to be inside your current directory and you call the main method from outside the package.
you should not set the classpath; that is completely unnecessary. Which partition are you working in? Don't work in the root directory; create yourself a Java directory and work in that.
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Campbel, actually I was getting the problem in some other program which was running without any problem via NetBeans but not via command prompt. So I just created the above code for test purpose to know what's going in background. So I was running/testing them in the d:\. After finding the solution, I took the reference of "The Complete Reference Java". I got my answer there "In order for a program to find the Package, one of three things must be true. Either the program can be executed from a directory immediate above the Package, or the CLASSPATH must be set to include the path to the Package, or the -classpath option must specify the path to Package when the program is run via java.
When the second two options are used, the class path must not include the Package, itself."
 
Robby Ames
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I've one more query. It is:

"if I doesn't create any Package for the class 'PackageTest' and import the class 'OnlyTest' as 'import p1.*;' then it gives a compile time error 'cannot access OnlyCla. bad source file .\OnlyCla. file does not contain class OnlyCla'. But it doesn't give any error if I import the class 'OnlyTest' as 'import p1.OnlyCla; I also doesn't give any error if I create the both classes in a same package or two different packages and import the OnlyCla as 'import p1.*;'

What's the reason behind this?
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The people who wrote the language prohibited imports without package names.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!