Java classpath: showing Java where the classes are
Java ClassPath...its purpose
Some of my posts are "what I have learned", others are more "what I am learning". I prefer the former, bu this one is more the latter.
The subject of classpath has been hard for me to internalize...more below on why (IDEs).
When I installed my JDK I needed to alter my environmental variables on my computers so I could run java from the command line. I had to add the location of my JDK to the path. Installation instructions include this point:
Include JDK's "bin" Directory in the PATH
They then show how to access the path and variables on Windows 10 and other platforms.
So, of course I already did that. with C:\Program Files\Java\jdk1.8.0_231\bin
But then I kept seeing Java documents, textbooks, tutorials saying I must add JARs or packages, or such to the classpath. I wondered why my projects were working without doing any of that.
I wondered about classpath because I had already done the above.
I wondered about classpath because I had already done the above.
Seems the answer is because except for the simplest projects I use IDEs such as Eclipse and InteliJ which handle such things for me. And confusion because many said classpath should/could also be done with OS environmental variables and looked like what I had already done.
One thing the above means is I need to spend more time, and do more projects using just the command line vs IDE runs. However, I did learn that what I did long ago is really for the operating system to run Java JDK and such. The newer direction I was given about "adding to classpath" is to show the JDK, JRE, etc where the classes are for my program. Especially other than those classes in the default directory/package.
One of the below resources explains that we can show Java where the classes are with -classpath command as in the below:
java -classpath "$CLASSPATH:nameOfFile.jar:lib/*" path.to.your.MainClass
As usual, knowing a concept and internalizing are different. The latter requires enough practice to make it second nature, so that is on the todo listThen, what if there are multiple directories of class files. Looks like manifest is needed as one resource stated:
We get around this problem by deploying a main jar file
myapp.jar
which contains a manifest (Manifest.mf
) file specifying a classpath with the other required jars, which are then deployed alongside it. In this case, you only need to declare java -jar myapp.jar
when running the code.
Comments
Post a Comment