Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

classpath default settings

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys.

I've been playing around a bit with the CLASSPATH env variable and the command line option and I've come to a conclusion, for which I would like to have confirmed if it is unversal or not (across different OSes).

Basically, I have found the following to be true, for both java and javac:

1) If -cp / -classpath is used at the commandline it will override the CLASSPATH env-variable (no news, this is true according to K&B too).
2) If -cp / -classpath is NOT used, and CLASSPATH is set, this will (obviously) be the classpath used.
3) If neither of the above are used/set (CLASSPATH is not set, nor is -cp used at the command line) then the classpath will default to your current working directory.

Number 3 has caused me a little headache. Since I had no idea it would be set to the current workingdirectory under the aforementioned circumstances, I kept wondering how the heck the class files could be found. This is also the rule I would like to have confirmed.

Basically, imagine we are given a test-question where CLASSPATH is not explicitly set, and nor is -cp/-classpath used. Is it then guaranteed that the current working directory will be used as classpath? Is this a guaranteed behaviour across different plattforms?

Regards,
Andreas.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas Svenkson wrote:
Basically, imagine we are given a test-question where CLASSPATH is not explicitly set, and nor is -cp/-classpath used. Is it then guaranteed that the current working directory will be used as classpath? Is this a guaranteed behaviour across different plattforms?
Regards,
Andreas.



Ikpefua wrote:


Hello Andreas... If I understand your question, I want to use a simple logic to give you an answer, it is java that searches for the .class files and NOT the underlying OS. To futher explain, a caption of the TWO minute drill page 809 of the K & B book summarises as follows:

Searching With Java And Javac (Objective 7.5)

-Both java and javac use the same algorithms to search for classes
-Searching begins in the locations that contain the classes that come
standard with J2SE.


If the above mentioned behaviour depends on different platforms I am very SURE
that it would have been EXPLICITLY mentioned. (Just as they EXPLICITLY
mentioned the forward(/) backward(\) slash differences in the OS).

I hope this helps.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you understand my question correctly, or maybe it is I who don't understand your answer.

But to clarify, on page 798 of K&B you can read the second sentence: "When searching for class files, the java and javac commands don't search the current directory by default."

What I am saying is that this is wrong (atleast from my point of view, on my OS), IF you assume that 1: you have NO CLASSPATH environment variable set, and 2: you do NOT invoke java/javac with the -cp/-classpath option - then your current directory WILL be the default classpath for java and javac.

So if you have the directory structure:

...x
... |-pack1
.........|-MyClass.class

(x is root directory, pack1 a subdirectory, MyClass.class is inside pack1 and has a package declaration of: package pack1;)

...and you are standing in directory 'x', then you can successfully execute: java pack1.MyClass
This is not a question. For me and my compiler/JVM this is a fact.

What I am wondering is if this is a guaranteed behaviour on all JVM's and compilers, even those implemented on other OS'es?

// Andreas
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas Svenkson wrote:Hi guys.

I've been playing around a bit with the CLASSPATH env variable and the command line option and I've come to a conclusion, for which I would like to have confirmed if it is unversal or not (across different OSes).

Basically, I have found the following to be true, for both java and javac:

1) If -cp / -classpath is used at the commandline it will override the CLASSPATH env-variable (no news, this is true according to K&B too).
2) If -cp / -classpath is NOT used, and CLASSPATH is set, this will (obviously) be the classpath used.
3) If neither of the above are used/set (CLASSPATH is not set, nor is -cp used at the command line) then the classpath will default to your current working directory.

Number 3 has caused me a little headache. Since I had no idea it would be set to the current workingdirectory under the aforementioned circumstances, I kept wondering how the heck the class files could be found. This is also the rule I would like to have confirmed.

Basically, imagine we are given a test-question where CLASSPATH is not explicitly set, and nor is -cp/-classpath used. Is it then guaranteed that the current working directory will be used as classpath? Is this a guaranteed behaviour across different plattforms?

Regards,
Andreas.




Your first 2 points are correct.
For 3rd, thinks are a little bit different. Your assumption is correct but only for javac. If neither CLASSPATH env variable is set, nor command line -classpatch/ -cp is used , the java command will return an error as by default it doesn't "look" into working directory. This should be specified explicitly.

Regards,
Dragos
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dragos Nica wrote:
Your first 2 points are correct.
For 3rd, thinks are a little bit different. Your assumption is correct but only for javac. If neither CLASSPATH env variable is set, nor command line -classpatch/ -cp is used , the java command will return an error as by default it doesn't "look" into working directory. This should be specified explicitly.

Regards,
Dragos



That's exactly how I thought it worked too. The problem is that this isn't the case (for me). If I "un-set" the CLASSPATH env var, and don't use -cp, then 'javac' and 'java' will both use my current working directory as its classpath!

Have you tried this yourself? If not, I'd appreciate if you would. On a windows system, just open a dos and write 'set CLASSPATH=' to unset it. Then try to execute a class: java mypack.MyClass, while standing in the parent dir to directory 'mypack'. Assuming your MyClass has a correct 'package mypack;' statement, this will work fine (atleast it does for me).

EDIT: I've also found an interesting question that touches on this topic. In K&B, chapter 10 question 11, where the correct answer is A: In my opinion, since there is no env-var mentioned, I think we can assume it's not set. Which means, the only reason that the 'java' command in answer A works, is because of rule number 3. Ofcourse, one could also assume that '.' is part of the env-var, in which case it's an entirely different story/reason why it works... but if it's not specifically mentioned, surely that can't be the reason?

// Andreas
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas Svenkson wrote:I don't think you understand my question correctly, or maybe it is I who don't understand your answer.

But to clarify, on page 798 of K&B you can read the second sentence: "When searching for class files, the java and javac commands don't search the current directory by default."

What I am saying is that this is wrong (atleast from my point of view, on my OS), IF you assume that 1: you have NO CLASSPATH environment variable set, and 2: you do NOT invoke java/javac with the -cp/-classpath option - then your current directory WILL be the default classpath for java and javac.

// Andreas



Ikpefua wrote:



Andreas,
I understand you PERFECTLY.
It is you that is 'twisting' what you read in page 798 of K & B. "When searching for class files, the java and javac commands don't search the current directory by default. This is DERIVED from the heading in page 797 which says quote "Declaring AND Using Classpaths" you can see clearly that the statement is ALL about the declaration and use of classpaths.
I will now ask you a question, why did say this is wrong? if the statement is ALL about 'declaring and using classpaths' ???.


Now lets come to your theory....(REMEMBER That has NOTHING to do with the headline in page 797)..... after searching the classes that comes with the j2se, and you are standing in your current directory -in the command line- and in that SAME command line you are telling java to run a class -in the current directory- it will do JUST that!. Now your question is.... is this behaviour guaranteed in other platforms, my answer is this (That I Already Said Before) If this behaviour depended on platforms, the writers of K & B book or the java tutorials provided by Sun would have EXPLICITLY mentioned it.

Let me answer in another way, If this behaviour is NOT guaranteed in other platforms, the K & B book or the java tutorials provided by Sun, would have EXPLICITLY mentioned it.

I await your reply...Thanks



 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas,
I understand you PERFECTLY.
It is you that is 'twisting' what you read in page 798 of K & B. "When searching for class files, the java and javac commands don't search the current directory by default. This is DERIVED from the heading in page 797 which says quote "Declaring AND Using Classpaths" you can see clearly that the statement is ALL about the declaration and use of classpaths.



I am not twisting anything. I have quoted what is written and have given you my interpretation while providing 2 conditions that must apply in order for my statement to be true.

I will now ask you a question, why did say this is wrong? if the statement is ALL about 'declaring and using classpaths' ???.



I've written clearly above how I am reasoning, if you still have trouble following I suggest you reread my posts. I've mentioned 2 circumstances which, when they are "true", in my humble opinion makes it incorrect to say that the current directory is not included by default. (Although I do see what the authors meant, I just find it slightly incomplete to phrase it like that)

Now lets come to your theory....(REMEMBER That has NOTHING to do with the headline in page 797)..... after searching the classes that comes with the j2se, and you are standing in your current directory -in the command line- and in that SAME command line you are telling java to run a class -in the current directory- it will do JUST that!. Now your question is.... is this behaviour guaranteed in other platforms, my answer is this (That I Already Said Before) If this behaviour depended on platforms, the writers of K & B book or the java tutorials provided by Sun would have EXPLICITLY mentioned it.



Yes, it will run the class in the current directory, even when you have no CLASSPATH env var defined and are not using the -cp/-classpath commandline option. That's my point. (underlined edit, first post was incorrect)

Let me answer in another way, If this behaviour is NOT guaranteed in other platforms, the K & B book or the java tutorials provided by Sun, would have EXPLICITLY mentioned it.



The problem with this last statement of yours is that the 2 points you brought up in your first post (1:"Both java and javac use the same algorithms to search for classes " and 2: "Searching begins in the locations that contain the classes that come standard with J2SE.") and which I assume you were referring to, has nothing to do with the topic of this thread. I have never questioned these statements, they just don't have anything to do with how the CLASSPATH var is set, or if the -cp option is used. These 2 points apply under all circumstances, but it has nothing to do with my post.

EDIT: I get the impression that you think these 2 points somehow imply that the current directory would be included when we search for classes, but I have no idea how you would come to that conclusion.

Regards,
Andreas.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Andreas
I am NOT a java guru, I am here to prepare for the exams, I PERFECTLY understand AND agree with what the K & B book explains in pages 797 and 798, regarding java AND javac behaviour in colaboration with -classpath or -cp in the command lines. If you are not satisfied -like you earlier mentioned- You can ask the authors to explain futher.

I repeat, if a particular behaviour VARIES from one platform to another it would have been specifically mentioned by the K & B book and Sun.

The issue of the default classpath being the current directory you are standing -I repeat- has NOTHING to do with pages 797 AND 798.
because they are talking EXCLUSIVELY about the use of -cp or -classpath.

let me talk with lines of codes:
___________________________________________________________________________________________
java MyClass (Your Theory)

java -cp . MyClass (Page 797 - 798 K & B Book 'Declaring And Using Classpaths')
___________________________________________________________________________________________

At this point I have a question, have you noticed the FUNDAMENTAL difference in both command lines??

What I am trying to explain is that in 'your theory' if the behaviour was platform dependent, be rest assured that
the K & B book would have mentioned it, I am sure that it is NOT platform dependent.
___________________________________________________________________________________________

Andres wrote:

The problem with this last statement of yours is that the 2 points you brought up in your first post (1:"Both java and javac use the same algorithms to search for classes " and 2: "Searching begins in the locations that contain the classes that come standard with J2SE.") and which I assume you were referring to, has nothing to do with the topic of this thread. I have never questioned these statements, they just don't have anything to do with how the CLASSPATH var is set, or if the -cp option is used. These 2 points apply under all circumstances, but it has nothing to do with my post.

Ikpefua wrote:


They may NOT have anything to do DIRECTLY with how the CLASSPATH var is set or if the -cp option is used. But it is you that QUESTIONED the use of the dot (.) in this SAME TOPIC and I simply explained why it is so.

-cp or classpath MEANS "Class-Search-Path" so if you say they(The Above Mentioned) dont have have anything to do with the topic,
its okay with you, but I am sorry that I simply do NOT agree with you.


 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas wrote:



But to clarify, on page 798 of K&B you can read the second sentence: "When searching for class files, the java and javac commands don't search the current directory by default."
What I am saying is that this is wrong (atleast from my point of view, on my OS), IF you assume that 1: you have NO CLASSPATH environment variable set, and 2: you do NOT invoke java/javac with the -cp/-classpath option - then your current directory WILL be the default classpath for java and javac.

Ikpefua wrote:


Andreas the above statement is NOT wrong, I guess what the book 'ought to have added' -IMHO- is that if the -cp flag is NOT used you, can run a class with the java command if you invoke the java command from the current directory the class is located.

There is a difference between a WRONG statement and an ADDITIONAL statement.

I am quite sure that at this point we agree with ourselves.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are ofcourse entitled to disagree, but so am I

And I do see what you mean by that the pages would concern CLASSPATH and -cp exclusively, but that's not how I look at it. I consider it a part of a bigger explanation concerning the overall functioning when java/javac searches for classes. Now that specific subtitle may be regarding "Declaring and using Classpaths", but it's all part of a bigger picture to explain how java and javac searches for classes, and it is in fact the only sentence that I have noticed which could at all be (mis)interpreted to apply in the situation which this thread is about.

Now, the reason I personally doubt what you believe so strongly, that the authors would have mentioned it if it varied between implementations, is quite simply this;

The behavior / searchpaths are not discussed specifically when neither CLASSPATH nor -cp is used. The book is focused on how java and javac operates with a CLASSPATH var and with the -cp cmd line option. It is simply not included in the chapter how java and javac behaves when neither of these are in effect - so why would the authors mention anything about a (small) topic they didn't include in the first place? Who would?!

Anyway the point of this topic is not to criticize the authors, they've done an amazing job and I know I'm not the only one to think so. The point here was to get some insight in this specific behaviour and get some kind of confirmation about how it works.

To get this topic back on track; I was under the impression that it worked like Dragos said, but having tried it I can only say that this is not the case for my implementation. For me, when CLASSPATH is not set and -cp is not used, the current working dir will be used as the classpath.

// Andreas
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andreas wrote:


For me, when CLASSPATH is not set and -cp is not used, the current working dir will be used as the classpath.

Ikpefua wrote:


Of course you are right! when -CLASSPATH is NOT set and -cp is NOT used, the current working dir will be used as
classpath sure!.
Read everything I have said in this thread, I did NOT mention that your theory is WRONG.
I was ONLY saying that there is a DIFFERENCE between your theory and that of the K & B book
And that you should NOT say they (K & B authors) are WRONG.

I simply wanted to make you understand that the book did NOT make any mistake, they simply did NOT go into full
details in many aspects of java, think of it, 5000 pages will NOT be enough to handle the world of java, so what they
did was to be as SPECIFIC as possible in COHERENCE with what will be tested for in the exams.

Thanks Andreas...It was a fabulous debate!! AND trust me NO Grudges!
 
Yes, my master! Here is the tiny ad you asked for:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic