Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic packages question

 
John Rumassin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 2 files, in two separate folders.

\myPackage\test.java
\myPackage2\test2.java

//test.java
package myPackage;

public class test {
public test()
{}
}

//test2.java
package myPackage2;

import myPackage.test;

public class test2
{
public static void main (String[] args)
{
System.out.println("hello");
}
}

i can compile test.java. when i try and compile test2.java it says "package myPackage does not exist". the error code then follows with the statement "import myPackage.test".

i only have one classpath which is "."

on another computer it works fine and on mine it doesn't! help, please!

this is a simplistic example and i know you can do classpath ".." but i want it work without this as it is not necessary on another machine (and in my complicated . my machine is windows 2000 with 1.5.11 and the machine it works on is xp with 1.6.0
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"java beginner"

Please go back and read our Naming Policy. At the bottom of that page, you will find a link to change your display name. This is one of the two policies here that is strictly enforced - the other being "Be Nice".

Once you have done that, i'm sure someone will be glad to help you with your problems.
[ April 13, 2007: Message edited by: Fred Rosenberger ]
 
John Rumassin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry. i changed my name will apply by the rules. john
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While compiling your test.java use below command:

javac -d . test.java

you will find myPackage\test.class file created.

Now compile your test2.java as

javac -d . test2.java

you will find myPackage2\test2.class file created.

Hope this helps
 
John Rumassin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the response. i will give it a try. i thought -d was for system properties? is this something with windows 2000 vs xp? why wouldn't i need the -d on one computer but i would need it on another? thanks in advance, john
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The -d option specifies where the class files should be created. It has nothing to do with your problem.
What directory are you in when you compile test2.java ?
If you only have "." in your classpath, then you need to be in the directory that the myPackage directory is a sub-directory of. If you are in the mypackage2 directory, then you need to specify that parent directory in your classpath.
e.g. if myPackage is a sub-directory of source, use the command line
javac -cp source myPackage2\test2.java

[ April 13, 2007: Message edited by: Joanne Neal ]
[ April 13, 2007: Message edited by: Joanne Neal ]
 
John Rumassin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the reply. i know that i can add -cp ".." and it will recognize the above directory and compile. but this is not necessary on one machine and it is necessary on the other. i have checked the environmental variables and found that the only directory is ".". i am wondering if it is not a classpath issue? the code above is all the code. if you have the following file structure, please try it:

c:\myPackage
c:\myPackage2

compile both files (without the use of defining the classpath). thanks to all for the help, John
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if THIS describes your situation ... under Using Packages.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic