Why does the code require reference to the current directory (i.e. :.) when launching class UseKit from the command-line using java command when the same is NOT required while compiling?
I believe we have to specify reference to the current directory in classpath using java and javac commands when we have to search for OTHER CLASSES (dependencies) to complete the operation. Pls. correct me if I'm wrong.
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
The -classpath parameter has nothing to do with .java files. It's used to find .class files only.
I believe we have to specify reference to the current directory in classpath using java and javac commands when we have to search for OTHER CLASSES (dependencies) to complete the operation. Pls. correct me if I'm wrong.
It depends...
For example, supposing you have inside c:\temp the .class file for the following class :
code:
--------------------------------------------------------------------------------
class HelloWord{ public static void main(String[] args){ System.out.println("Hello Word"); }}
--------------------------------------------------------------------------------
If you run from within c:\temp directory the command java HelloWord, no problem. java.exe will be able to find your class.
class JavaWorld {} You will compile and run this class using following commands from the current directory: c:\temp>javac JavaWorld.javafollowed by...c:\temp>java -classpath . JavaWorld It won't and shouldn't run (atleast on my machine) using: c:\temp>java JavaWorld It throws an exception saying "Exception in thread "main" Java.lang.NoClassDefFoundError: JavaWord"
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
You can compile and run in two ways from within c:\temp directory :
1st.
javac HelloWorld.java
java HelloWorld
2nd.
javac HelloWorld.java
java -classpath . HelloWorld
But if your class imports any other classes which you have to inform in classpath, compulsorily you have to inform the . in your classpath in order to allow java.exe find your main class.
How can you see a "Exception in thread "main" java.lang.NoClassDefFoundError: JavaWord" message if your command line is java JavaWorld ?
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|