This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question classpath 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 "Question classpath" Watch "Question classpath" New topic
Author

Question classpath

Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Hi Friends,
Question from masterexam

I could not understand why E and H are the correct alternatives, if someone can give me a strength in that part of the classpath would be grateful, is the subject which will enable me to be ready for scjp
Sorry bad grammar because I'm from Brazil
Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Hi Vinicius, welcome to javaranch.

Option H has a mistake in it, /mp/jars/cp.jar should be /mp/jars/mp.jar. Classpath is explained here...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vinicius Moreira
Greenhorn

Joined: Jan 18, 2010
Posts: 27
Thanks Ankit Garg for answer
I'm reading the link you gave me but still do not understand how it works right, is the subject remaining to complete my studies for the test scjp
Thanks for answer
Juliet Smith
Greenhorn

Joined: Apr 12, 2010
Posts: 3
I agree that H contains a mistake, but not with Ankits reply (the jar filename is correctly shown in option H as mp.jar and not cp.jar as he states)

I think H should read java -cp /jars/mp.jar player.MusicPlayer

(The question states "You are currently in the directory /mp")

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello Juliet,

Welcome to Javaranch.

Happy Preparation,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Kaspar Christenson
Greenhorn

Joined: Feb 26, 2009
Posts: 21

Since you're on a UNIX system
java -cp /jars/mp.jar player.MusicPlayer
would look for the jars directory under the root folder.
You need to get ride of the forward slash before jar to get this command line to work.


It helps to understand the classpath if you remember that for the .class files it is enough to give the path to the directory that contains the directory with the same name as the first part of the package declaration.

To use a JAR file you have to give it the name of the file. Giving it the name of the directory containing the JAR file will not work.
Juliet Smith
Greenhorn

Joined: Apr 12, 2010
Posts: 3
So H should read:

java -cp jars/mp.jar player.MusicPlayer ?

(Given you are currently in the directory /mp)
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
I have two questions about this quiz question (Quiz A number 68) too:

My first question is that Option "F" is:

java -cp /mp/classes/player MusicPlayer

F is incorrect, and the answer says it's incorrect because F "has the wrong classpath".

I thought it would be correct to say:

java -cp /mp/classes player.MusicPlayer

...but it sounds like there's something different wrong? What would be the correct statement if you wanted to use the absolute path?

My second question is that the answer says that the CLASSPATH is no good to us.

The CLASSPATH is:

/mp/jars

Would one correct CLASSPATH be:

/mp/jars/mp.jar ?

and another correct CLASSPATH be:

/mp/classes ?

Thanks in advance for help!
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 762
Katherine,


If you are under a directory where there is /mp/classes, then you should use this command. This command means go to /mp/classes to find player.MusicPlayer.
For example, if you have d:/myproject/mp/classes/ where player.MusicPlayer is and your current directory is d:/myproject, then use this command.
But make sure MusicPlayer is under player package.
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
Himai Minh wrote:


If you are under a directory where there is /mp/classes, then you should use this command. This command means go to /mp/classes to find player.MusicPlayer.
For example, if you have d:/myproject/mp/classes/ where player.MusicPlayer is and your current directory is d:/myproject, then use this command.
But make sure MusicPlayer is under player package.


Hi Himai,

Thanks for helping me with this question so quickly! The problem says that I am currently in the directory /mp. Does this mean that
would indeed work for invoking the class player.MusicPlayer? I think this is what your answer said, but I wasn't sure if I was understanding it correctly.

This is the file structure that was given for the problem:



Thanks again!
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 762
Katherine.
If your current directory is in mp, use this

java -cp classes player.MusicPlayer

That means the it will search through the classes directory to find player.MusicPlayer class to execute.

Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
Is it possible to use the absolute filepath in this case? If it isn't, when can the absolute file path be used? I am a little confused when, why, and how it is used.
Daniel Alexander
Greenhorn

Joined: Jun 20, 2012
Posts: 4
@Himai / Anyone

I'm new to this forum and relatively new to java also, and trying to get my head around the class path question in this post. I have a few questions:

1 - Option e is stated as being correct - "E - java -cp classes player.MusicPlayer". i understand that by saying -cp player.MusicPlayer, your telling the system to use the classpath directory and then append player to this directory, what i'm unsure of is why you use a "." to separate player and the MusicPlayer file and not a "/", or am i confusing unix and windows commands here? I'm not a command line user tbh. Also what is the reason or signifcance of the word "classes" before the "player.MusicPlayer" part? The only assumption i can make is that because there are both .java and .class files in the same directory, you are telling the system to search only classes, is this correct? If so why is it "classes" rather than "class"?

2 - Option H is also stated as being correct - "H - java -cp /mp/jars/mp.jar player.MusicPlayer". This is where my lack of command line knowledge is perhaps confusing me. If by -cp you are telling the system to go to the directory as defined in the class path, and then append the the following classpath to that directory, wouldn't this be telling the system to find a directory that is mp/jars/mp/jars/mp.jar player.MusicPlayer, which doesn't exist?

Hope i haven't been to confusing with my questions.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 762
For javac, the format is like this :
javac -cp classpaths file_you_will_compile.java

The classpaths is the directory where the java files that are needed in order to compile file_you_will_compile.java
You need to specify the physical directory where file_you_will_compile.java is.

For example, you have a A.class in a directory c:\myProject\classes. B.java uses an instance of A. You have B.java to compile. B.java is in c:\myProject\application folder.
The javac command is:
javac -cp c:\myProject\classes c:\myProject\application\B.java


For java, the format is like this:
java -cp classpaths class_you_will_run

The classpaths includes the directory where class_you_will_run is.

Use the same example in Window operating system,
java -cp c:\myProject\classes;c:\myProject\application application.class_you_will_run

You need to include application. in front of class_you_will_run if the class is under the application package.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question classpath