aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes import and package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "import and package" Watch "import and package" New topic
Author

import and package

Jeffry Cray
Greenhorn

Joined: Oct 08, 2000
Posts: 25
Hi all,
I've posted this question about 2 weeks ago but still I couldn't get an answer for making it work !!
//you have two seperate files
//you can compile and run the pack class, but when running the imp class it objects to the import statement at compile time.....Can anyone please tell me, why this is happening..!?
//the first is C:\test\pack.java
package test;
public class pack {
public void printPack() { System.out.println("from test package"); }
public static void main(String[] args) {}
}

//this is the second C:\test\imp.java
import test.*;
public class imp {
public static void main(String[] args) {
pack p = new pack();
p.printPack();
}
}
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
Please copy/paste the exact error message that you got.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Tim Clendenen
Greenhorn

Joined: Oct 14, 2000
Posts: 3
I don't know why but in Windows Java has a problem compiling and importing a class in the same directory that you are working in.
Try this, it worked for me.
To Compile:
1. In your DOS window go to the directory that contains test.
2. On the command line enter: javac test\imp.java
After doing this it compiled fine but I still had trouble running it. I then moved imp.java to the directory that contains test\ I recompiled and ran it, and it worked fine.
Hope this helps.
Tim
[This message has been edited by Tim Clendenen (edited October 14, 2000).]
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
I've recently downloaded JDK 1.3 and now my compiler is updated. My OS is win98 and these are the new problems ...not including the old ones...!!
When I DON'T put "package test;" in the "pack.java" source file it compiles and runs correctly.
When I put "package test;" as the first line in my "pack.java" source file, it'll compile correctly but when run I get the following !!!
C:\test>java pack
Exception in thread "main" java.lang.NoClassDefFoundError: pack (wrong name: test/pack)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass (Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal (ClassLoader.java:313)
..I know I told you pack.java was working but when I updated to JDK1.3, I'm getting this new problem...
I'll leave the imp.java right now so I can solve the pack.java problem, any suggestions ?? becuaes I'm really stuck !


america
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
Earlier, I used to get such problems because the compiler and the interpreter both get confused if you do not keep the class files in the proper directories. It is a good idea to seperate the source file directory from the class files. This is how I organize my works directory and this has never created any problems for me. Of course, there are other solutions to your problem but IMHO, this probably is a very good way...
c:\works\java : contains all my java source code files.
c:\works\classes : All the .class files go here (in appropriate directories)
I keep "c:\works\classes" in my classpath.
I compile the java files by using the following command (using a bat file though) :
javac -d c:\works\classes XXX.java
Now, you've a java file (pack.java) with a package name 'test'.
c:\works\java>javac -d c:\works\classes pack.java
This will direct the class file to go to c:\works\classes\test automatically.

HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern) www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited October 19, 2000).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Paul ... Thank you, Thank you, Thank you!!!
I've been having the same problem with JDK1.3 under Win98 and it's been driving me crazy. Your solution worked like a charm
------------------
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
ali farid
Ranch Hand

Joined: Aug 26, 2000
Posts: 60
Hi Jeffry
The solution is very simple.
the first file C:\test\pack.class
which contains the following statement should be placed in a directory test, just like u did.
package test;
and the other one imp.class should be placed outside of that dir.
like this
C:\imp.class
just place the java files in the respective directories and compile and run them.
Claude Rouleau
Greenhorn

Joined: Oct 13, 2000
Posts: 9

I beleive tha this is about "How java find a class" and the "CLASSPATH" environment variable.
See. if I have no CLASSPATH set and I try the following I get an error.
C:\test>javac imp.java
imp.java:2: Package test not found in import.
import test.*;

Then I set the CLASSPATH variable one level below the package name. I get no problem to compile.
C:\test>set classpath=C:\
C:\test>javac imp.java
C:\test>java test.pack
Note that you have to get the full name class variable (including package name).
Hope that this help.
Jeffry Cray
Greenhorn

Joined: Oct 08, 2000
Posts: 25
Hi all,
Thanks alot for helping me out. I really understand it now.
what I did was
1 c:\test>set classpath=.;c:\
2 c:\test>javac -classpath c:\test
3 c:\test>javac pack.java
4 c:\test>javac imp.java
5 c:\test>java imp
note that, I put all files "pack.java, imp.java, pack.class and imp.class" in the same direcory which is "c:\test" !!
What actually does the trick is line 2 !!
and it worked perfectly!!
Thanks alot, for without your help I wouldn't have been ANYWHERE...
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
I am working in a directory called c:\jdk1.3\bin, and have tried the last two suggestions in this topic. I am creating the package with an interface, and trying to import the package in the program that implements it. I've tried these last two suggestions, and this is the result.
C:\jdk1.3\bin>javac InterClass.java
InterClass.java:5: cannot resolve symbol
symbol : class doug
location: package java
import java.doug;
^
InterClass.java:6: cannot access Inter
bad class file: c:\jdk1.3\bin\Inter.class
class file contains wrong class: java.doug.Inter
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
class InterClass implements Inter {
Is there any hope? Thanks, Doug
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Douglas,
Don't give up. It will work . Re-read Paul Anil's instructions in the earlier post about making seperate source and class directories. You can make them within c:\jdk1.3\bin if you want but you don't have to.
Once you've created your directories and set your classpath, compile from within your source directory and remember to use javac -d [class directory path]. This instructs the compiler to place the class file in the given directory AND create any package directories if necessary.
Hope that helps.

------------------
Jane
Jock Mursh
Greenhorn

Joined: Oct 20, 2000
Posts: 1
As per the Claude Rouleau discussion it worked but when i try java imp
getting a message as Can't find class imp.
Iam not still clear of what's going on.
Can anybody make it clear.
Why are we saying as set classpath=C:\
Douglas Wolfinger
Ranch Hand

Joined: Aug 28, 2000
Posts: 57
eheheheh
Finally got it to work. I followed Paul's advice except for the command to type the directory name before the program name, when compiling the program that imports the package. I just typed javac -d (pgmname.java), and it went in to the directory where the package is, and imported it.
[This message has been edited by Douglas Wolfinger (edited October 21, 2000).]
[ July 27, 2002: Message edited by: Douglas Wolfinger ]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Douglas,
Glad you finally got it working How did you manage to get away with typing java -d prgname.java and have it go to the right place?? When I do that the compiler returns with no errors but no class file is generated??
Jock ...
I've posted detailed instructions here http://webhome.idirect.com/~jgriscti/win98.html.
Hope they help.
I'm not sure why everyone has such trouble with it under Win98; at work I use NT and everything worked smooth as silk. Maybe time to upgrade at home, if only I could be sure all the games would work
------------------
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import and package