File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpaths Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpaths" Watch "Classpaths" New topic
Author

Classpaths

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164



Question is as follows:

Given that the MusicPlayer project is on a UNIX system and consists of the following files:
/mp/player/MusicPlayer.java
/mp/classes/player/MusicPlayer.class
/mp/jars/mp.jar
Inside mp.jar file the structure is:
player/MusicPlayer.java
player/MusicPlayer.class
You are currently in the directory
/mp
and the CLASSPATH is set to
/mp/jars
What command(s) can you use to invoke the class player.MusicPlayer? (Choose all that
apply.)
Correct Answer
E: java -cp classes player.MusicPlayer
H: java -cp /mp/jars/cp.jar player.MusicPlayer



Firstly, am i correct in assuming that the boled text in the above question should be mp.jar and not cp.jar?

Secondly,

How is answer E correct? The JVM cannot access the MusicPlayer.java file.


OCPJP 6, OCMJD
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Yes. You're right. It should be mp.jar instead of cp.jar.

I guess MusicPlayer is in declare in player package.

java -cp classes player.MusicPlayer is right because player.MusicPlayer is the fully qualified name of this class.
java executes .class file, not .java file.
On the other hand, javac compiles .java file to make a .class file.

java goes to classes path and find the MusicPlayer.class. Then, it executes this class file.


Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Helen Ma wrote:Yes. You're right. It should be mp.jar instead of cp.jar.

I guess MusicPlayer is in declare in player package.

java -cp classes player.MusicPlayer is right because player.MusicPlayer is the fully qualified name of this class.
java executes .class file, not .java file.
On the other hand, javac compiles .java file to make a .class file.

java goes to classes path and find the MusicPlayer.class. Then, it executes this class file.




So the JVM only need to know the whereabouts of the .class file (and its dependant class files) to run?

In other words, are .java files are only used to compile?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Yes.
Here are the most confusing difference between javac and java because people nowadays like to use JBuilder/Eclipse/ NetBean to compile. No one cares about java or javac command lines. When I was in college 12 years ago ,Java 1 or 2 was in use and my school did not have those IDE installed, I have a lot of pain dealing with -classpath command.

But when I start working in the industry, people develop software using more than 100 hundred classes, for example, using javac or java is not convenient at all.

This is a very classical topic to deal with javac or java.

javac -cp __1____ ______2_______

What is in blank 2? The file(s) you need to compile with .java extension, for example B.java
What is in blank 1? The class(es) root path or jar file which B.java will use for compilation.

java -cp __1___ ____2_____

What is in blank 2? The class you need to execute, for example B.java
What is in blank 1? The class(es) root path or jar file where B resides and all other class(es) that B needs

Do you see the difference between blank 1 in javac and the blank1 in java?
In javac's blank 1, you only need to put the paths to the class files that B needs.
In java's blank 1, you need to put the paths of B and the paths to the class files that B needs.
This is the most confusing point.

For some other example, please refer to previous post 2 days ago about classpath.

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, Glen, take a look at the "Classpath related question" posted by C Halbe for further reference.
Correct anything if you feel I am wrong.

How can I link that post to the text? Let me know if you know how.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

java -cp classes player.MusicPlayer is right because player.MusicPlayer is the fully qualified name of this class.


I still do not see how this works. The default classpath is /mp/jars

in the jars directory, there is no directory named 'classes'.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Glen Iris wrote:
java -cp classes player.MusicPlayer is right because player.MusicPlayer is the fully qualified name of this class.


I still do not see how this works. The default classpath is /mp/jars

in the jars directory, there is no directory named 'classes'.

If you use the -cp option then the CLASSPATH environment variable is completely ignored. The classes directory would have to be a subdirectory of the current directory (i.e. /mp) which it is.


Joanne
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Hi Joanne,

The classes directory would have to be a subdirectory of the current directory (i.e. /mp) which it is


From what I see, the classes directory referred to in your above quote will is the one contained within the JAR file. So is this acceptable to use without mentioning the actual JAR file?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Glen Iris wrote:From what I see, the classes directory referred to in your above quote will is the one contained within the JAR file. So is this acceptable to use without mentioning the actual JAR file?

No. The classes directory I refer to is the one in the filesystem. From the original question
Given that the MusicPlayer project is on a UNIX system and consists of the following files:
/mp/player/MusicPlayer.java
/mp/classes/player/MusicPlayer.class
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Thank you Joanne for following through on this.

The classpath is set to /mp/jars

If we use E "java -cp classes player.MusicPlayer " that means that we the make the classpath = "/mp/jars:classes"

1 - Is my above statement correct?

2 - If my above statement is correct, contained within "/mp/jars:classes" must be a directory named player containing a file named MusicPlayer.class. However the contents of "/mp/jars" is: mp.jar

and

there is no /classes directory relating to "/mp/jars:classes"

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Glen Iris wrote:

The classpath is set to /mp/jars

If we use E "java -cp classes player.MusicPlayer " that means that we the make the classpath = "/mp/jars:classes"

1 - Is my above statement correct?

2 - If my above statement is correct, contained within "/mp/jars:classes" must be a directory named player containing a file named MusicPlayer.class. However the contents of "/mp/jars" is: mp.jar

and
there is no /classes directory relating to "/mp/jars:classes"


Regarding to 1. I think according to Joanne, if you set "java -cp classes", it means your class path is classes only, not /mp/jars.
Regarding to 2. I think if you do this "java -cp /mp/jars:classes" means your classpath is /mp/jars and classes. /mp/jars is the absolute directory,just like c:\mp\jars in Windows OS.
So, java will find MusicPlayer.class from /mp/jars. If it is not found, it will search through classes folder to find it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Helen Ma wrote:Regarding to 1. I think according to Joanne, if you set "java -cp classes", it means your class path is classes only, not /mp/jars.

Correct

Helen Ma wrote:Regarding to 2. I think if you do this "java -cp /mp/jars:classes" means your classpath is /mp/jars and classes. /mp/jars is the absolute directory,just like c:\mp\jars in Windows OS.

Correct again

Helen Ma wrote:So, java will find MusicPlayer.class from /mp/jars. If it is not found, it will search through classes folder to find it.

Not quite (assuming you mean it will search the jar files in /mp/jar). If you specify a directory in the classpath, java will only look in the file system for class files. It will not look in any jar files that are in that directory. If you want to include jar files in the classpath they have to be specifically included. i.e.
java -cp /mp/jars/mp.jar:classes

You could also include all the jar files in a directory using
java -cp /mp/jars/*:classes
Note that the wildcard is *, NOT *.jar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpaths
 
Similar Threads
Another Error in Master Exam...
Mock Exam Question Doubt, 36: 74 from K&B CD
Possibly misleading Master Exam question
Mock question on classpath
Question classpath