aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpaths with packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpaths with packages" Watch "Classpaths with packages" New topic
Author

Classpaths with packages

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Hi,

Is it possible to use the -classpath option when compiling a .java file which has a package and import statement?


OCPJP 6, OCMJD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19062
    
  40

Glen Iris wrote:
Is it possible to use the -classpath option when compiling a .java file which has a package and import statement?


Sure. Why would it be not possible?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

I cannot get it working.

c:/glen/com/scjp/dir
..........................|--Dog.class
..........................|
..........................|
..........................|--lower
....................................|-Animal.class


From the c:/glen directory, if I type "javac /com/scjp/dir/Dog.java", it compiles fine. What should I type to compile Dog if I want to use the -classpath instruction too? I am aware that this will work without the -classpath command but I want to prove to myself that it will work with the -classpath too.


Dog.java
}

Animal.java
Kaur Manpreet
Ranch Hand

Joined: Feb 14, 2012
Posts: 30

Hi Glen,

Assuming the following directory structure

Try the following commands:

Make the current drectory as source

To run the Dog class from the same directory (source)

I have used cp option in both the javac and java command

Hope I managed to clear your doubt
Thanks


Rajat kumar jain
Greenhorn

Joined: Dec 22, 2011
Posts: 25
Ok glen so let me make some effort in clearing your doubt regarding your question....

please follow the following points

During compiling you should be in the directory where your .java file is located. If you are not in that directory then you should give a complete path so that compiler comes to know where is your .java file located.

For eg in your question you didnot specify but i think your Dog.java file is located in geln\com\scjp\dir\Dog.java. Now you should be either in C:\ directory and provide the path \glen\com\scjp\dir\Dog.java and you have to tell the compiler where is your class files that you are using in your program are located that is you have to tell it -cp \glen(javac -d \glen -cp \glen Dog.java) . The other option is that you set your path to C:\glen\com\scjp\dir> javac -d ..\..\.. -cp ..\..\.. Dog.java.
You know what does 3 .. implies. ".." implies that take me out of the current directory. so first .. takes you to scjp second .. takes it to com and third .. takes it to glen where your class file glen\com\scjp\dir\lower\Animal.class is located. So in this way you can use -cp in javac. Hope i am clear to you. I have also spent sleepless nights on this concept hope this is clear to you. If you need any other help then i am always there for you.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Glen.
Assume your current directory is still c:\glen.
If you want to compile with -classpath, you may want to try this:
javac -classpath com\scjp\dir com\scjp\dir\Dog.java

Javac will look for the lower.Animal under com\scjp\dir.

But before you do that, make sure you compile Animal.java and make sure Animal.class will be under
com\scjp\dir\lower directory.

To compile Animal.java
javac com\scjp\dir\lower\Animal.java
This will create Animal.class under com\scjp\dir\lower
Rajat kumar jain
Greenhorn

Joined: Dec 22, 2011
Posts: 25
Helen Ma wrote:Hi, Glen.
Assume your current directory is still c:\glen.
If you want to compile with -classpath, you may want to try this:
javac -classpath com\scjp\dir com\scjp\dir\Dog.java

Javac will look for the lower.Animal under com\scjp\dir.

But before you do that, make sure you compile Animal.java and make sure Animal.class will be under
com\scjp\dir\lower directory.

To compile Animal.java
javac com\scjp\dir\lower\Animal.java
This will create Animal.class under com\scjp\dir\lower


But the compiler will try to search it with the fully qualified name com.scjp.dir.lower.Animal.java. But if you will take it into dir then it wont be able to search com.scjp.dir.lower.Animal.java and will give you an error. So please check your statement again and please correct me if I am wrong
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Rajat kumar jain wrote:
Helen Ma wrote:
Assume your current directory is still c:\glen.
If you want to compile with -classpath, you may want to try this:
javac -classpath com\scjp\dir com\scjp\dir\Dog.java


Let me correct the javac command:
javac -classpath . com\scjp\dir\Dog.java
In the current directory, it will search for com.scjp.dir.lower.Animal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpaths with packages