Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question classpath

 
Vinicius Moreira
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Vinicius Moreira
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Juliet,

Welcome to Javaranch.

Happy Preparation,
 
Kaspar Christenson
Greenhorn
Posts: 21
Mac Scala VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So H should read:

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

(Given you are currently in the directory /mp)
 
Katherine Reut
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1310
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1310
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 1310
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic