• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can not run a java program that start with package statement

 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am close to submit, I was using JBuilder as Editor, I was getting confused when I start testing my application under JDK1.5.0_07. To investigate the problem, I tried to run a very simple program,the
following:

public class Package1 {
public Package1(){
System.out.println("I am in package1");
}

public static void main (String[] args) {
new Package1();
}
}

This program run without any problem, but if I add the statment
"package suncertify;" at the begining of the file, I will have un error message "wrong name : suncertify/Package1", the class Package1.class is within a folder called "suncertify", the parent folder of suncertify is mentionned in the CLASSPATH,

Could you help me on fixing this problem

Best Regards
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the following directory structure:



where . is the current working directory,



ie. try specifically modifying the classpath (-cp .). Hope that helps.
[ June 23, 2006: Message edited by: Peter MacMillan ]
 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter MacMillan:
With the following directory structure:



where . is the current working directory,



ie. try specifically modifying the classpath (-cp .). Hope that helps.

[ June 23, 2006: Message edited by: Peter MacMillan ]
 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter

My current working directory is c:\TestJava\suncertify

CLASSPATH contains c:\TestJAVA

I run the program as follows
java Package1

is there anything wrong with this
Thanks in Advance
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Tilling:


I run the program as follows
java Package1



you need to specify the fully qualified name of Package1, not just the name of the class. So:

set CLASSPATH=c:\TestJAVA
java suncertify.Package1

and this assumes that Package1.class is c:\TestJAVA\suncertify\Package1.class

note that you can run this *anywhere* in your filesystem as you are explictly setting a classpath.
 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter
It works now!

I have another question:

If I have a class Data1 in the package suncertify, and the class Data2 in the package suncertify.db, if I want to use Data2 from Data1, does Data1.java must have to include "import suncertify.db" statement?
Actually I could not use Data2 from Data1 even when I inserted the statement "import suncertify.db" in the file Data1.java

please advice

Best Regards
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Tilling:

If I have a class Data1 in the package suncertify, and the class Data2 in the package suncertify.db, if I want to use Data2 from Data1, does Data1.java must have to include "import suncertify.db" statement?


You don't have to import, but you do have to qualify class names.

So (from the perspective of Data1):

import suncertify.db.Data2;
or
import suncertify.db.*;

Will let the compiler know where to find the Data2 class. Without an import statement, you would have to use "suncertify.db.Data2" wherever you want to use the class (sometimes necessary in naming conflicts).

Originally posted by Mike Tilling:

Actually I could not use Data2 from Data1 even when I inserted the statement "import suncertify.db" in the file Data1.java


That import statement is meaningless (as it only imports the db package name and not the classes - ie. import suncertify.db.*; or import suncertify.db.Data2; ) .
[ June 23, 2006: Message edited by: Peter MacMillan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic