This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compiling java with import package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiling java with import package" Watch "compiling java with import package" New topic
Author

compiling java with import package

Richard Mendoza
Ranch Hand

Joined: Feb 26, 2003
Posts: 48
Hi,

I have created two different package.

1. com -> example -> web, which contains Example1.java

2. com -> example -> model, which contains Example2.java that makes use of Example1.java therefore has an import com.example.web.Example1 on the first line.

In compiling Example1.java, I just go to the subdirectory com\example\web and issue javac Example1.java. And it worked fine.

In compiling Example2.java, I just go to subdirectory com\example\model and issue javac Example2.java. And there goes the error.

How do I compile this without using special IDE but rather compiling only by hand?

Thanks!
Richard Mendoza
Ranch Hand

Joined: Feb 26, 2003
Posts: 48
Hi,

This is related to my previous post above. I forgot to mention the error I was getting. I am getting an error of "package does not exist".
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When compiling Example2.java, include a classpath telling the compiler where to find the import.

For example, if your directory structure is c:\myjava\com\example\web, and you're importing com.example.web then provide a class path of c:\myjava when compiling...

javac -classpath c:\myjava Example2.java

See this section from Thinking in Java for more detail.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Richard Mendoza
Ranch Hand

Joined: Feb 26, 2003
Posts: 48
Hi Marc,

To complete the path here it is,

For Example1.java, it's located in
C:\MyProjects\SamplePrg\com\example\web\Example1.java

For Example2.java, it's located in
C:\MyProjects\SamplePrg\com\example\model\Example2.java

As you notice, Example1 is in ..\web package and Example2 is in ..\model package.

Compiling Example1.java is no problem...

But compiling Example2.java is what I get an error.. Because it makes use of Example1's instance so I use the import...

I tried what you did in your sample post.. And it did remove the error specifically for package in ..\web but I now get "package does not exist" for other packages like java.util, javax.servlet, etc..
Richard Mendoza
Ranch Hand

Joined: Feb 26, 2003
Posts: 48
Hi Marc,

To complete the path here it is,

For Example1.java, it's located in
C:\MyProjects\SamplePrg\com\example\web\Example1.java

For Example2.java, it's located in
C:\MyProjects\SamplePrg\com\example\model\Example2.java

As you notice, Example1 is in ..\web package and Example2 is in ..\model package.

Compiling Example1.java is no problem...

But compiling Example2.java is what I get an error.. Because it makes use of Example1's instance so I use the import...

I tried what you did in your sample post.. And it did remove the error specifically for package in ..\web but I now get "package does not exist" for other packages like java.util, javax.servlet, etc..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compiling java with import package