Reputation: 133
I write a source file and put that public class in a package :
package abc;
public class Employee
{
// Constructor
public Employee(String name, double salary)
{
this.name = name;
this.salary = salary;
}
// Methods
public String getName()
{
return this.name;
}
public double getSalary()
{
return this.salary;
}
// instance field
private String name;
private double salary;
}
Then I try to compile it using command: javac Employee.java , it generates a .class file in the same directory as the source file
Now I try to use this package, so I write a source file :
import abc.*;
public class HelloWorld
{
public static void main(String args[]){
//System.out.println("hello world");
Employee aEmployee = new Employee("David",1000);
System.out.println(aEmployee.getName() + aEmployee.getSalary());
}
}
I try to compile it using: javac HelloWorld.java , but it has a error says : package abc doesn't exist
I have the following questions:
1) Why did this error happen ?
2) How to solve this problem ?
3) Each time when I package some classes, where can I find the package to use afterwards
I've read some docs about this, but that's so complex, can somebody explain it simply ?
Upvotes: 2
Views: 7418
Reputation: 1420
A few things you need to do:
Put class files for the abc package into a folder called abc. E.g.
mkdir abc; mv Employee.class abc
See comments below/other answers for using the -d option instead.
Tell the java compiler where to find the class files. You do this with the -classpath option. If abc
is in your current folder, then you can tell javac to search for class files in . like so:
javac -classpath . HelloWorld.java
To answer your first question: the error happened because the java compiler didn't know where to look for the abc package. The second is answered above. And these steps should help in the future :).
Upvotes: 0
Reputation: 6738
You should compile likes this
javac -cp d:\yourJavaProject\abc Employee.java
javac -cp d:\yourJavaProject\abc HelloWorld.java
and run likes this;
java -cp .;d:\yourJavaProject\abc HelloWorld
See reference to learn about compile and run Java program in package from command line.
Upvotes: 0
Reputation: 33544
Try it out like this..
javac -sourcepath /path/to/srcdir -cp /path/to/libraries -d /path/to/outputdir
Check this site for good tutorial:
http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
Upvotes: 0
Reputation: 666
If you are working with class files . You should have your class files in the same folder structure as the packages are.(Its the practice) For example if "abc" is the package . Then your Employees.java should be in an "abc" folder. After compilation the class file that gets generated will fill fall into abc folder. In any case you should make sure that your class file is in abc folder.
Then after, if you are try to use abc.Employees.class in another class. You must keep the abc folder in class path e.g. if your abc folder is in d:/java/sources/bin/abc .. this is where you have your employees class.
else
complile with -d option
javac Employee.java -d d:/java/sources/bin
set classpath=d:/java/sources/bin/:%classpath%` .
javac HelloWorld.java
After this you will be able to compile your code.
Upvotes: 1
Reputation: 143906
1) Why did this error happen ?
This is because the java compiler looks for a directory tree when it tries to load a package, either in the classpath or in a jar file. This means for a package called abc.foo.bar
, it will look for the directory tree: /abc/foo/bar
and expect classes that belong to that package to be there. You've compiled your Employee class but when you import it, the compiler looks for a directory abc
in your classpath, and it's not there.
2) How to solve this problem ?
You need to make sure when you compile the Employee class, its classfile is in a directory abc
which is somewhere in your classpath. The simplest thing may be to create a directory called abc
, then move the Employee.java file into the abc
directory, then compile:
javac abc/Employee.java
This will create a Employee.class file in the abc
directory. Then you can compile your HelloWorld:
javac HelloWorld.java
3) Each time when I package some classes, where can I find the package to use afterwards
In the directory tree that you've named your package. See the later part of the response to 1)
.
Upvotes: 4