[Logo]
Forums Register Login
Question classpath
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
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...
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
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")

Hello Juliet,

Welcome to Javaranch.

Happy Preparation,
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.
So H should read:

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

(Given you are currently in the directory /mp)
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!
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.
 
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!
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.

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.
@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.
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.

Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 23, 2017 23:04:25.