How do I import user defined packages to my java application.
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??
Re: How do I import user defined packages to my java application.
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.
Re: How do I import user defined packages to my java application.
Quote:
Originally Posted by
Norm
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.
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"
Re: How do I import user defined packages to my java application.
Quote:
the class is in the package world. And I have set the classpath as C:\world.
The classpath should point to the folder containing the package path: C:
the world folder should not be part of the classpath.
Re: How do I import user defined packages to my java application.
Quote:
Originally Posted by
Norm
The classpath should point to the folder containing the package path: C:
the world folder should not be part of the classpath.
I have tried this also but this didn't work!!
Re: How do I import user defined packages to my java application.
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:
Code java:
package world;
public class Example
{
public static void main(String [] args)
{
System.out.println("This is an example from Zaphod_b.");
}
}
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
Re: How do I import user defined packages to my java application.
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:
Code java:
package world;
public class Example
{
public static void main(String [] args)
{
System.out.println("worldExample.main(): This is an example from Zaphod_b.");
}
public static void printMessage()
{
System.out.println("world.Example.printMessage(): This is an example from Zaphod_b.");
}
}
Compile with javac as before.
Create a new Java source file:
Code java:
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
Code java:
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