1

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?

1
  • 1
    java and javac command executes from anywhere when you add its java library folder path into PATH variable. Try echo $PATH. Commented Apr 24, 2013 at 5:09

2 Answers 2

1

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/.

Sign up to request clarification or add additional context in comments.

4 Comments

@user2056245 No problem. Also, I just found the version 7 documentation for the javac command: docs.oracle.com/javase/7/docs/technotes/tools/windows/…. Something about a Solaris version, too, towards the bottom of the page if you click the javac link on the last documentation link in my answer. I'm not sure if the Windows or Solaris version is more applicable to Linux.
but how should i compile and run two classes that are in different folders just using the terminal and java-javac-(-cp) commands?
@user2056245 Well, there's a couple options. Assuming your current directory is folder1, you could call javac -cp ./folder2 ./myprogram.java. Then you would run the program by calling java -cp .;./folder2 myprogram. (I'm a bit rusty on Java. I don't think you need the file extension when you call java, and the . for current directory may be optional.)
If you want to keep that other file in its folder, though, you probably want to make it a package. Here's a tutorial on making packages: docs.oracle.com/javase/tutorial/java/package/index.html. With a package where the top folder is in the current directory, you don't need to specify the classpath at all, I don't think.
1

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.