I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??
Is the class in a package? Is the package path on the classpath when the program is compiled?
Please post the text of the import statement and show the command line when the javac command is issused and show the folders and paths when javac is executed.
If you don't understand my answer, don't ignore it, ask a question.
Yes the class is in the package world. And I have set the classpath as C:\world.
In the first attempt i used the import statement as "import world.Balance"
In the second attempt i used "import Balance"
But none of them worked. I got the same error in both of the attempts.
The error was that "world.Balance.class cannot be accessed,bad class name"
The classpath should point to the folder containing the package path: C:the class is in the package world. And I have set the classpath as C:\world.
the world folder should not be part of the classpath.
If you don't understand my answer, don't ignore it, ask a question.
Here is what I did on my Windows XP workstation:
I have a folder F:\home\Zaphod
In F:\home\Zaphod I created a file named Example.java
Here's what is in that file:
Here is the session where I compiled and executed: Computer output is blue, my input is black.
F:\home\Zaphod>dir C:\world
Volume in drive C has no label.
Volume Serial Number is 843A-0C08
Directory of C:\world
10/11/2012 10:42 AM <DIR> .
10/11/2012 10:42 AM <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 6,137,946,112 bytes free
F:\home\Zaphod>javac -d C:\ Example.java
F:\home\Zaphod>dir C:\world
Volume in drive C has no label.
Volume Serial Number is 843A-0C08
Directory of C:\world
10/11/2012 10:43 AM <DIR> .
10/11/2012 10:43 AM <DIR> ..
10/11/2012 10:43 AM 447 Example.class
1 File(s) 447 bytes
2 Dir(s) 6,137,946,112 bytes free
F:\home\Zaphod>java -cp C:\ world.Example
This is an example from Zaphod_b.
Note that if I didn't already have a directory named C:\world, it would create an empty directory at the time I executed the javac command as shown above on a source file that has a package world; line.
Note, also, that Windows ignores case in file names and directory names, but Java does not (that's not) ignore case in class names and package names.
So, for this class name, Example, the following does not work:
java -cp C:\ world.example
Java gives a very informative Error message that starts:
Exception in thread "main" java.lang.NoClassDefFoundError: world/example (wrong name: world/Example)
Also, since the package name is world, it won't work if you try:
java -cp C:\ World.Example
Etc.
My setup:Windows XP Service pack 3
javac.exe and java.exe version 1.6.0_33
YMMV (Your Mileage May Vary)
Now, stop here if any of this stuff doesn't work or doesn't make sense on your system. It's got to be right before we get to the Good Stuff.
If you want to call a function in the world package rather than just executing a main() function from a particular class in the package, see my next post.
Cheers!
Z
Last edited by Zaphod_b; October 11th, 2012 at 05:07 PM.
If everything in my previous post worked for you, then try the following to be able to invoke a method in a package class.
Change Example.java to the following:
Compile with javac as before.
Create a new Java source file:
package world; import world.*; public class UseExample { public static void main(String [] args) { Example.printMessage(); } }
Compile with javac in a similar manner.
Here's my session with the new Example.java and UseExample.java:
F:\home\Zaphod>javac -d C:\ -cp C:\ UseExample.java
F:\home\Zaphod>javac -d C:\ -cp C:\ Example.java
F:\home\Zaphod>java -cp C:\ world.UseExample
world.Example.printMessage(): This is an example from Zaphod_b.
F:\home\Zaphod>java -cp C:\ world.Example
world.Example.main(): This is an example from Zaphod_b.
If you want UseExample to be in a different package, then you can try something like
package anotherworld; import world.*; public class UseExample { public static void main(String [] args) { Example.printMessage(); } }
Use exactly the same javac command line, but now it will create UseExample.class in C:\anotherworld, and you can execute it with the following command:
java -cp C:\ anotherworld.UseExample
Cheers!
Z
Last edited by Zaphod_b; October 11th, 2012 at 02:21 PM.