What is the difference between Path and ClassPath in Java? - java

Why do we need Path and ClassPath?
When using IDE's like eclipse we still need to add path ?

We don't need to set PATH and CLASSPATH to compile and run java program while using IDE like Eclipse.
These environment variables are required to compile and run java program using CMD.
Example-: Here is the screen shot of console to understand PATH and CLASSPATH quickly
Explanation-:
Compiling the program-
I have java program file Demo.java stored at location D:\Programs\Classes. Now I pointed location to D:\Programs\Classes in CMD and executed javac Demo.java command. System will understand javac with the help of PATH variable. Java program Demo.java is complied successfully because PATH is set correctly to %JAVA_HOME%\bin.
Running the program (class file)-
Since class file has been generated at the same location D:\Programs\Classes, so we can run this class file by typing command java Demo as displayed in second line in the screenshot. Now system will find the class file with the help of CLASSPATH since my CLASSPATH variable has D:\Programs\Classes path.
It's not required to point class file location in CMD to run it. System will understand java command with the help of PATH variable and find that class using CLASSPATH variable to run it.

path is a mediator between developer and operating system to inform binary file path
where as Classpath is a mediator between developer and compiler to inform the library file path those are used in our source code

The path points to the location of the jre i.e. the java binary files such as the jvm and necessary libraries. The classpath points to the classes you developed so that the jvm can find them and load them when you run your product.
So essentially you need the path to find java so it can then find your classes and run them from the classpath

let us clear the difference in points:
PATH
a) An environment variable which is used by the
operating system to find the executables.
b) PATH is nothing but setting up an
environment for operating system. Operating
System will look in this PATH for executables.
c) Refers to the system
CLASSPATH
a) An environment variable which is used by the
Java compiler to find the path, of classes i.e in
J2EE we give the path of jar files.
b) Classpath is nothing but setting up the
environment for Java. Java will use to find
compiled classes.
c) Refers to the Developing Enviornment.

The main difference between PATH and CLASSPATH is that PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.
For more info: http://www.java67.com/2012/08/what-is-path-and-classpath-in-java-difference.html

path : it is location of bin files(binary executable files)
example- java.exe,javac.exe
classPath: it is location of your .class file(which is created after compile your java source file)

Path and Classpath both are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.

Difference between path and classpath
Difference between path and classpath in Java
path is set for use java tool in your java program like java, javac, javap.
javac are used for compile the code. and classpath are used for use predefined class in
your program for example use scanner class in your program for this you need to set classpath.
http://www.tutorial4us.com/java/difference-between-path-and-classpath

PATH is the environment variable where we specify the locations of binaries.
Example: We add bin directory path of JDK or JRE, so that any binaries under the directory can be accessed directly without specifying absolute path.
CLASSPATH is the path for Java application where the classes you compiled will be available.

Related

What is the purpose of setting a CLASSPATH in Java?

I just installed Java on a Mac for the first time (jdk 10.0.2 SE) and I'm on macOS 10.13.6. I just created a basic Hello World program in my home directory, compiled it and ran it, but I have nothing set on my CLASSPATH environment variable except for the current directory "." My question is how am I able to compile and run this program? How is it finding the classes from the Java library? I've always assumed I need to set this variable, and I have it set to my jdk installation directory in Windows.
WHY DO WE SET THE CLASS-PATH?
well this is an riveting question.
it is self explanatory as
CLASSPATH is an environment variable and contain
paths to the jar files and path to various packages.
KISS ANSWER TO YOUR QUERY:
Perhaps ,the reason behind successful execution of
your code is that the JVM checks the current directory
first for the perception of jar files and then follows
the classpath .So your current directory has those files.
OH!You may wonder then why would we set the classpath
.Setting the classpath overrides that by default path.
Happy Learning :)

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

Are both the JRE and the JDK required to run a JAR file?

It is possible to run a JAR file locally. The next step is to run it on a different PC.
The question is whether the JRE, the JDK or both are required to run the JAR file?
The JDK contains the JRE.
Most program only need the JRE (Java Runtime Environment) but some programs need the Compiler at runtime in which case you need the JDK.
If you have the JDK, you don't need the JRE as well.
To run a jar file you only need java.exe(windows). JDK is the development kit for Java and JRE is the runtime. JDK contains JRE.
In the comments on the accepted answer nobalG asked, "Why the compiler is needed if jre is already there?"
At the time of writing I did not have enough reputation to comment, so I replied here instead.
I had a situation where I wanted to write code that compiles other code at runtime and then uses that compiled code. In my case I was creating a tool that could take a test class based on a particular framework, compile it, load the class, and extract test data from it so that the data could be used as part of an end-to-end test. In order for this tool to run properly it must be run with the JDK so that it can use the Java compiler.
To run a jar file you only need the JRE. You can run the jar file with the following command:
java -jar [jar file Name]
You only need JRE.
If the jar file you are trying to run has the Main-Class: <classname> header present in manifest file, then you can simply run the jar file by the command:
java -jar [your jar file name]
If the manifest file does not have that entry (and you know the fully qualified class name of the class containing main function), then you can run the jar file by the command:
java -cp [absolute path to jar file] [full qualified class name containing the main function]
JRE is enough to run
JDK is used for development
You need a JRE but not the JDK. The JRE is the java runtime environment and java code cannot be executed without it. The .jar is a compiled java file can and this needs the java runtime environment to be run.
You want to run the jar file; so you just need Java Runtime environment ( i.e. JRE).

Exception in thread "main" java.lang.NoClassDefFoundError:after setting PATh and CLASSPATH

I have tried to write a program where i want to access class in a jar.
I am using netbeans as ide,Windows 7 os and have added jar in the libraries.
But i keep getting the error.
Exception in thread “main” java.lang.NoClassDefFoundError:after setting PATh and CLASSPATH
I have set the PATh variable to my <jdk directory>/bin;
CLASSPATH to %CLASSPATH%;
and JAVA_HOME to <jdk directory>
<jdk directory> =C:\Program Files\Java\jdk1.7.2
You've just learned an important lesson: Java ignores CLASSPATH environment variable.
You need the PATH to your JAVA/bin for your own convenience, but the CLASSPATH is not useful. I don't have one on any machine I work with.
You can see why: they're so idiosyncratic and specific to a given project.
You should learn how to set it using -cp option on javac.exe and java.exe. You can use Ant to build for more complex projects. And if you deploy to a Java EE app server, you need to understand the classloader hierarchy.
If you insist on using NetBeans, I'd recommend looking in the help to see how it wants you to set CLASSPATH.
You should have the jar file in your classpath to access the classes inside it. Moreover when you run a program from an IDE it overrides any classpath settings you do at system level. Please check your build path in the IDE.

How to set environment variables for javac to be able to find imported packages?

I am not a java developer. I just want to run a java application (which can be downloaded from:
http://code.google.com/p/k-shortest-paths/downloads/list
, under this name: KShortestPaths_Java_v2.1.zip)
While trying to compile test\edu\asu\emit\qyan\test\YenTopKShortestPathsAlgTest.java
I get "package ... does not exist" and "symbol ... does not exist" which I know are related to path setting. Can you please tell me how I should set environment variables and from which directory compile and run that java file?
(My operating system is Windows XP and I have saved the application in C:\KSh)
Edit:
I resolved the problem with compiling. Now, I have a CLASS file: YenTopKShortestPathsAlgTest. However, when I try to run it with java, I get this error: "could not find the main class... program will exist"
which I guess is again related to the paths other jar files are located. Could you please kindly give me a hint?
You need to point the classpath to the name of the .jar files, and/or the name of the directory containing your class files e.g.
CLASSPATH=c:\dir\myjar.jar;c:\classes
so you list the .jars required and the directories involved, separated by semicolons. You can either set the CLASSPATH environment variable, or use the above directly with javac thus:
javac -cp c:\dir\myjar.jar;c:\classes {source files}
The zip file contains a .classpath and a .project file. These files are used by the eclipse java IDE.
Perhaps the most easy way would be to download eclipse and import the project there.
If you want to do it by hand, try
javac -sourcepath src;test test\edu\asu\emit\qyan\test\YenTopKShortestPathsAlgTest.java
from your directory C:\KSh.
EDIT:
Download junit.jar and add it to the classpath with
javac -classpath junit.jar -sourcepath....

Categories

Resources