Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages

 
rajajee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jexperts;
how to run the main() in the following class?
package myPack;
public class TestPack {
public static void main (String [] args){
System.out.println("HelloWorld");
}
}
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajajee
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must put your source file in a subdirectory called myPack.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And then compile it with javac. Finally, run it with
java myPack.TestPack
The fully qualified named is mandatory for packaged .class files.
-anthony
 
Sal Velinus
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anthony Villanueva:
And then compile it with javac. Finally, run it with
java myPack.TestPack
The fully qualified named is mandatory for packaged .class files.
-anthony

Not if you're in the myPack directory when you run the program,or is that wrong?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go up one level then run
java myPack.TestPack
-anthony
 
rajajee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Anthony and all others;
Thanks for your replies. It does not work, however. Please try to compile and run the code yourself and then please let me 'a beginner' know your expert comments.
Once again thanks a lot.
Rajajee
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, suposse your java source home directory is c:\java. Suposse your classpath environment variable is set to CLASSPATH=c:\java.
If you make a dir: c:\java\TestPack and put your TestPack.java file in it and then compile it, you�ll have a TestPack.class file.
If you try to execute your class in c:\java\TestPack dir as shown:
java TestPack
the JavaVirtualMachine will find your class
(At least, my JVM could)
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works just fine.
Prints out HelloWorld.
hmmm. . . If you say the TestPack is in packaqe myPack, then you have to be sure that it IS in myPack. Go look in that directory and see if the TestPack.class file is actually there. If not, put it there (it is not nice to lie to the JVM).
Then go to the directory that the myPack folder is in and run
>java myPack.TestPack
 
Sigfred Zamo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I�m wrong, Cindy is correct.
It�s necesary execute your class in the root directory with his full name
dir1.dir2. (..) .yourclass
having your packaged class in the root/dir1/dir2/ .. /yourclass.class directory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic