Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

package

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. classpath and package
Suppose I created a file - Test.java which has a package statement in it,
package MyPackage;
Of course, I have such a directory. I created Test.java at the directory- c:\NS\java\MyPackage>
I compiled the file like this :
c:\NS\java\MyPackage>javac Test.java , It compiled.
When I listed files and directories, giving
c:\NS\java\MyPackage>dir - it showed Test.java and Test.class files
Now, to run the program, I give
c:\NS\java\MyPackage>java MyPackage.Test - it runs ok.
The class path was set as this :
classpath = .;c:\jdk1.2.2\lib;c:\NS\java
Package statement specifies the place that particular class to be placed. Yes, it did. Now why do I have to mention the 'packagename' at the command-prompt to run it ?. I saw the class when I listed using 'dir' ?. And I am in that directory now, I mean, at the prompt c:\NS\java\MyPackage>
Is there anything wrong with my classpath setting ?.
Somewhere, I found that when you compile, put -d option. I tried that too. I did not see any difference. Instead, I think, it created another new MyPackage directory inside my MyPackage directory and put the Test.class file there.
Please help me...
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are sitting in the package directory when you invoke the class file, you do NOT need the prefix. When you are sitting in a different directory you name the starting directory(package) as a prefix to the class file to execute.
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy.
But I tried it, it's not working.
At the command line, c:\NS\java\MyPackage>javac Test.java
It is ok.
c:\NS\java\MyPackage>java Test.java
shows the error Exception in thread "main", NoClassDefFoundError
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soory, I forgot to add some more.
When I give the package name as prefix, it is working. I have no idea what is happening.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!