Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiling java with import package

 
Richard Mendoza
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Mendoza
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic