File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package question

 
Madan, Gopal
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 compilation units:
First...
package mypack;
public class one {}
Second...
package mypack;
public class two {}
In the Third...
import mypack.*;
public class Test {
new one;
new two;
}
The third compilation unit Test.java doesn't work as it is not able to find classes one and two. But, instead if i import them as follows:
import mypack.one;
import mypack.two;
Test.java compiles!!!???
Am i doing something wrong? Classes one and two are directly under the folder "mypack" and no sub-directories in between.
Thanks for any help
Madan
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
I couldn't reproduce your problem. I have one.java & two.java in a folder named mypack. These files have the same package statement
package mypack;
Then up above one directory level I have Test.java which has the following import statement;

import mypack.*;
Test compiles with the classpath set to .
I then run Test and its able to find the one & two class files just fine.
Maybe by describing what I did you can figure out what's going wrong in your situation.
Let us know if you get it to work!

------------------
Bill Gloff
Sun Certified Programmer for the Java� 2 Platform
 
Madan, Gopal
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill:
My 3 source code files are as follows:
one.java
[CODE]
package madan;
public class one {
private String test = "Hello from class one";
public String getValue() {
return test;
}
}[\CODE]
two.java
[CODE]
package madan;
public class two {
private String test = "Hello from class two";
public String getValue() {
return test;
}
}[\CODE]
Driver...Test.java
[CODE]
//import madan.*;
import madan.one;
import madan.two;
public class Test {
public static void main(String[] args) {
one o = new one();
two t = new two();
System.out.println(o.getValue());
System.out.println(t.getValue());
}
}[\CODE]
I have all these 3 java source files in a folder called JavaSrc
All my class files are dispensed in a folder called JavaCls, accomodating any package statement.
My classpath ENV variable:
C:\>set classpath
classpath=.;E:\JavaSrc\TIJ2;E:\JavaCls;
This way I can run the java program from anywhere.
Because of the way my one.java and two.java use package madan statements, the one.class and two.class are dispensed under a new folder called madan under E:\JavaCls. Test.class is under E:\JavaCls, as it does not have a package statement
I compile them like this:
E:\JavaSrc>javac -d E:\JavaCls one.java
E:\JavaSrc>javac -d E:\JavaCls two.java
E:\JavaSrc>javac -d E:\JavaCls Test.java
To run this example, I just say "java Test" from anywhere, excluding quotes.
This works, only if I comment the
import madan.*;
and use
import madan.one;
import madan.two;
as shown in Test.java .
Hope this explains my predicament.
Thanks
madan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic