user2056245
user2056245

Reputation: 595

some basic concepts regarding JAVA and Linux: PATH variable in LINUX and class location by JVM

I am new to Java programming and Linux environment. And I am finding it difficult to understand few things about what is classpath, how does JVM locate classes, and JAVA API and many other things.

For example , today I wrote two simple classes 1)employee and 2) employeetest and placed them in the same folder. (employeetest has the "main" method and uses employee in its code.) I compiled employeetest and then executed it using "javac" command. I saw that , employee.class was also added to the folder. So does this mean that JVM automatically compiles all those files that are required for execution?

Then i placed the employee class outside the current directory , and then tried to execute employeetest. Now I got an error regarding ClassNotFound!! why is it so? why didn't JVM search for the employee class in other directories?

The directory where I placed employee is also on my classpath or "PATH" in my linux? technically it should search for other directories also that are there in the PATH ?

correct me if I am wrong, because I am reading so many things on internet, I am not able to figure these concepts out clearly?

SO where does JVM search for the classes? In the same directory where the class with "main" is located?

On my machine when i do echo $JAVA_HOME nothing prints. but still my java and javac commands execute properly? why is it so? what is the meaning of $JAVA_HOME? where is JDK located? what are its functions?

Upvotes: 1

Views: 199

Answers (2)

jpmc26
jpmc26

Reputation: 29934

PATH and classpath are two very, very different things. PATH is a machine specific environment variable that the operating system (a Linux distribution, in this case, but Windows uses the same environment variable) uses to find executables. Executables include binary programs and some script files in Linux. Unless you are specifying the full, absolute path of javac or javac is in the current directory, PATH is how Linux finding the javac binary.

Class path, on the other hand, is Java specific. It can be set as an environment variable CLASSPATH or as an argument to the java executable like so:

java -classpath /some/dir:/some/other/dir myprogram

This is the set of directories where the JVM looks for class files OR packages (folders with a particular structure that contain class files), aside from the built in API.

Yes, the Java compiler does compile dependent source files if it can find them and determines that the matching class file is missing or out of date. The compiler will first search on the "sourcepath" argument if specified, and it will search on the class path as well. You may find it helpful to read over the command's documentation: javac. (That's for version 6. I couldn't find version 7, but I think all that applies.) Here is the documentation for java.

The JDK's and JVM's locations depend on where they were installed. Try which javac to find where the JDK is and which java to find the runtime; this will show where Linux is finding those executables (which it is probably doing via PATH).

I spent quite a bit of time rooting around through Java's documentation in my college career, and I gleaned a lot from it. You may find rooting around a bit yourself worthwhile. Here's the link: http://docs.oracle.com/javase/7/docs/.

Upvotes: 1

cafebabe1991
cafebabe1991

Reputation: 5186

Here are few basics of java/java compiler

You write java code ---JVM loads class file , the class file is the bytecode that actually makes java more portable(platform independent)

  • Your situation You compiled the source inside a folder say "foo" so for you to be able to compile it from anywhere you should provide a path to the class file

so javac -classpath somepathtothatfile

use the export command to set the path to that location where you now have the class file then that error will be removed

like export CLASSPATH="pathtosomelocation"

Jvm looks for the files inside its bin directory in windows its -c://programfiles/java/jdk(version)/bin/

in linux

/usr/lib/jvm/somejavaversion/bin

check it out

TWO THINGS FOR JAVA DEVELOPMENT

JRE-this is just the runtime things ,only to run JDK-that you need to develop code,to get the access to API you need .

Upvotes: 1

Related Questions