dekoay
asked on
Java Package
Hello Experts,
I am a junior Java Developer. Wondering if someone can help me on how to use a package that I created.
For example the code below:
package test;
public class A {
public void Printing("hello");
}
I save this file as A.java and compile it to a class file called A.class
Next, I jar it to test.jar and i set a classpath point to test.jar
Then I create a file called B.java with the following code:
package test;
public class B {
public void main(String args[]){
A a = new A();
a.Printing();
}
}
When I compile this B.java, I encounter this problem:
cannot resolve symbol
symbol : class A
location: class test.B
So, can u tell me how I can compile B program using test package successfully... Your help will be appreciated
dekoay
I am a junior Java Developer. Wondering if someone can help me on how to use a package that I created.
For example the code below:
package test;
public class A {
public void Printing("hello");
}
I save this file as A.java and compile it to a class file called A.class
Next, I jar it to test.jar and i set a classpath point to test.jar
Then I create a file called B.java with the following code:
package test;
public class B {
public void main(String args[]){
A a = new A();
a.Printing();
}
}
When I compile this B.java, I encounter this problem:
cannot resolve symbol
symbol : class A
location: class test.B
So, can u tell me how I can compile B program using test package successfully... Your help will be appreciated
dekoay
You have to compile A.class into a subdirectory test. The jar should contain test/A.class (verify with "jar tvf test.jar").
Compiling B.java, the classpath must contain either the directory holding test/A.class or the jar.
Running B.java you must have in your classpath the directory holding "test/B.class" and "test/A.class", or the directory holding "test/B.class" and the jar containing "test/A.class", or the jar containing "test/B.class" and "test/A.class".
Cheers,
--gandalf.
Compiling B.java, the classpath must contain either the directory holding test/A.class or the jar.
Running B.java you must have in your classpath the directory holding "test/B.class" and "test/A.class", or the directory holding "test/B.class" and the jar containing "test/A.class", or the jar containing "test/B.class" and "test/A.class".
Cheers,
--gandalf.
ASKER
Hi Experts,
My source code is as below:
Filename A.java
import java.io.*;
public class A
{
public A()
{
}
public void printing()
{System.out.println("Hello World!");}
}
Filename B.java
import test.*;
public class B
{
public static void main(String[] args)
{
A obj=new A();
obj.printing();
}
}
I compile A.java and manage to jar it as well. I also manage to compile B.java by using the package test.jar.
Unfortunately when I try to execute B, it gave me this error:
NoClassDefFoundError: test/A at B.main(B.java:7)
What is wrong with my code in B.java? Your help will be appreciated.
My source code is as below:
Filename A.java
import java.io.*;
public class A
{
public A()
{
}
public void printing()
{System.out.println("Hello
}
Filename B.java
import test.*;
public class B
{
public static void main(String[] args)
{
A obj=new A();
obj.printing();
}
}
I compile A.java and manage to jar it as well. I also manage to compile B.java by using the package test.jar.
Unfortunately when I try to execute B, it gave me this error:
NoClassDefFoundError: test/A at B.main(B.java:7)
What is wrong with my code in B.java? Your help will be appreciated.
How about putting a
package test;
as the first line in each of the two files?
--g.
package test;
as the first line in each of the two files?
--g.
ASKER
Hi Gandalf,
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
ASKER
Hi Gandalf,
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
ASKER
Hi Gandalf,
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
Thanks for the response. I have tried as u mention. But the problem still occur.. Dun know where goes wrong with this simple code. Hope u can show me how to solve it.
Thanks
Dekoay
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Place your java files into a 'test' directory as I stated in my original post. And ensure your classpath is set as mentioned.
eg.
If you keep your java files in a directory c:\java, then A.java and B.java should go in the following directories:
c:\java\test\A.java
c:\java\test\B.java
You should include c:\java in your classpath.
To jar them up you should be in the directory c:\java and use the command:
jar cvf test.jar test
eg.
If you keep your java files in a directory c:\java, then A.java and B.java should go in the following directories:
c:\java\test\A.java
c:\java\test\B.java
You should include c:\java in your classpath.
To jar them up you should be in the directory c:\java and use the command:
jar cvf test.jar test
You also need to ensire your jar file contains matching directory structure.