• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classpaths

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic