| Author |
Doubt regarding Classpath
|
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Please correct me if I am wrong? The following package struture is available on Windows 2000 I thought javac & java use the same searching patterns for compiling and runing the file. So if the following compilation works fine quote ------------------------------------------------- C:\foo>javac FooSample.java ------------------------------------------------- then the following invocation of the file should have worked fine, but it didn't Got the error quote ------------------------------------------------------------------- C:\foo>java FooSample Exception in thread "main" java.lang.NoClassDefFoundError: FooSample (wrong name: foo/FooSample) ------------------------------------------------------------------- And the reason I guess is - the JVM Searches for a file FooSample without a package which is not present and hence error but why did the compilation succeed in the first place? Can anyone please explain me? Really struggling a lot to understand classpaths properly [ April 01, 2008: Message edited by: Prem Vinodh ] [BPSouther: Added code tags to preserve indents in directory listing] [ April 01, 2008: Message edited by: Ben Souther ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
With or without a package is not a problem. java looks into your CLASSPATH. If the "C:\foo" is not there, java will not find FooSample. There are different ways to do it. One would be to explicitly set C:\foo in the CLASSPATH : "java -cp C:\foo FooSample". But what you probably want to do is set the "current path" into the CLASSPATH. In this case, you can use "java -cp . FooSample". javac does not need it, as it automatically looks into the current path.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Is my explanation of the same wrong? quote : ----------------------------------------------------------------- And the reason I guess is - the JVM Searches for a file FooSample without a package which is not present and hence error but why did the compilation succeed in the first place? ------------------------------------------------------------------ I even tried the following invocation quote ------------------------------------------------------------------ C:\foo>java -cp . FooSample ------------------------------------------------------------------ and got the following error Exception in thread "main" java.lang.NoClassDefFoundError: FooSample (wrong name : foo/FooSample)
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
wrong name : foo/FooSample
foo was a package !! Sorry, I didn't notice it at first place. Then you need to call your main class using its full package, and the folder at the top of this package must be in the CLASSPATH : -> calling foo.FooSample -> C:\ must be in the CLASSPATH. Once again, there are a few ways to do it, one of them would be to call "java -cp c:\ foo.FooSample". Java will look into C:\ for a class called foo.FooSample.
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Thanks for the reply Christophe So you mean to say the following invocation fails quote --------------------------------------------- C:\foo>javac FooSample.java C:\foo>java FooSample -------------------------------------------- because of the following reason "The JVM Searches in the folder foo for a file FooSample without a package which is not present and hence error" where as the following invocation succeed -------------------------------------------- C:\>java foo/FooSample Foo Package C:\>java foo.FooSample Foo Package --------------------------------------------- [ April 01, 2008: Message edited by: Prem Vinodh ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
"java FooSample" because FooSample is in a package and you must invoke the main class via its fully qualified name (foo.FooSample).
where as the following invocation succeed
Only if C:\ or the current directory(".") is in the CLASSPATH. Otherwise it will fail.
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Understood :-) What is the difference between the following invocations -------------------------------------------- C:\>java foo/FooSample Foo Package C:\>java foo.FooSample Foo Package --------------------------------------------- Are they same? Because both give me the same result. And what is the problem with the following invocation (the foo + "backward slash" + FooSample) quote : ----------------------------------------------------------- C:\>java foo\FooSample Exception in thread "main" java.lang.NoClassDefFoundError: foo\FooSample (wrong name: foo/FooSample) ----------------------------------------------------------- It has given me an error. I am working on windows so I thought the "\" between the foo package and the filename FooSample should work. Please explain what is wrong with it?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
Are they same? Because both give me the same result.
Yes, there are same, but I prefer the more javaish notation, using dots to separate packages.
Please explain what is wrong with it?
It simply is not a correct way to write it Java does not support it.
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Finaly, I have one last doubt quote ------------------------------------------------------------------------ C:\foo>javac FooSample.java// Compiles fine C:\>java foo/FooSample// Works Fine Foo Package // Prints Fine ------------------------------------------------------------------------ The above worked fine and so on similar lines, I was in foo folder and so to run FooSample I thought giving this ../ (look at the below quote) would bring me to the C:\ drive and then it should be the same as the above invocation, foo/FooSample quote ----------------------------------------------------------- C:\foo>java ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample ----------------------------------------------------------- Is this also a problem with setting the classpath. If so I also tried these invocations but none worked. quote : ------------------------------------------------------------------- C:\foo>java -cp c:\ ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample C:\foo>java -cp c: ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample C:\foo>java -cp c:/ ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample C:\foo>java -cp .. ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample C:\foo>java -cp ../ ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample ------------------------------------------------------------------- Please explain?
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
I just wanted to tell that the question posted by me was not taken from any mock exam or any other exam for that matter. It was just a sample which I was trying on my system in order to understand classpaths and packages. I wish to thank Christophe Verre, without whose help I would not have understood this topic. With Christophe Verre help I have understood this topic very nicely. Still is there something else I need to know on classpaths or is this sufficient for the exam Thanks a lot Christophe Verre for your kind help Greak work keep it up Thanks Java Ranch
|
 |
yu yong
Ranch Hand
Joined: Mar 09, 2008
Posts: 44
|
|
|
Learning a lot by this topic. Good questions and good explanations.
|
Yours sincerely,<br />yuyong<br /> <br /> <br /> E-mail:yuyong22@hotmail.com<br /> msn: yuyong22@hotmail.com<br /> Skype:yuyong88
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
Finally, I managed to run the below quote successfully. quote ----------------------------------------------------------- C:\foo>java ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample ----------------------------------------------------------- This should have been given as follows : quote ----------------------------------------------------------- C:\foo>java -cp .. foo/FooSample Foo Package ----------------------------------------------------------- But still I am not sure what the reasons are for the above quote not working properly. Can anyone please explain it to me? Thanks Everybody, Thanks Java Ranch
|
 |
Prem Vinodh
Ranch Hand
Joined: Mar 19, 2008
Posts: 33
|
|
I think I got the answer quote ----------------------------------------------------------- C:\foo>java ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample ----------------------------------------------------------- The problem with the above invocation was : the classpath was foo and so ../foo/FooSample would mean java searches for foo/FooSample by going one folder up from foo or in other words the current folder is foo and ../foo/FooSample means ../foo cancel each other and so this is now similar to c:\foo>java FooSample i.e. searches for FooSample without a package in the directory foo Now if the same had to be given as follows quote ----------------------------------------------------------- C:\foo>java -cp .. ../foo/FooSample Exception in thread "main" java.lang.NoClassDefFoundError: ///foo/FooSample ----------------------------------------------------------- The problem here was : My classpath was set to c:\ so ../foo/FooSample would mean one directory up from C:\ and then search for foo/FooSample which was not there and hence the error. And hence the correct way of invocation here would be : quote : ------------------------------------------------------ C:\foo>java -cp .. foo/FooSample Foo Package ----------------------------------------------------- which means my classpath is set to one folder above the current folder ie. C:\ and so java now searches for foo/FooSample from C:\ and hence works fine. Thanks Everybody, Thanks JavaRanch. [ April 03, 2008: Message edited by: Prem Vinodh ] [ April 03, 2008: Message edited by: Prem Vinodh ]
|
 |
 |
|
|
subject: Doubt regarding Classpath
|
|
|