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 :)
Related
In this link, I read:
Making changes to the system PATH variable is typically not necessary
for computers running Windows
What does "typically" mean?
When is it necessary to insert the java directory in the path and when is it not ?
In my company in some PCs the java command is recognized even if the java directory is not present in the path variable, in some others is not, with no evident difference between the two groups of PCs.
You want to use the PATH when you are using a jdk from the command line (ex. compileing, making jar)
When you make a java script, you want to compile your code, you want to use PATH to reference the java bin folder in the JDK. (Your path varable might look like this: set path=%path%;C:\Program Files\Java\jdk1.8.0_72\bin)
Answer:
If you are not making java programs and using the command line to compile/run/debug them, you do not need to set PATH variable.
Hope this helps! ☺
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.
This is probably the most frequent question you get in the world, and I apologize, but I have to ask anyway. I recently downloaded the newest version of java (1.7.0_45-b18), and I recently finished making a small program for a local community of mine in Eclipse. I'd like to share it with them so anyone can run it by clicking it, but opening the jar file just hasn't seemed to work for me.
I attempted opening it with command prompt by using
java -jar StatCalc.jar
but it always tells me
'java' is not recognized as an internal or external command, operable program or batch file.
I looked up many solutions for this, the most common being to change your Path. So I went to the environment variables, and changed the path to
C:\Program Files (x86)\Java\jre7\bin\java.exe
But it still gives me the same result. Only when the directory is exactly in java, and the jar file is in the java bin folder can I run the program. I wan't any person to be able to run this.
How do I get command prompt to work and allow this program to be run by both me and other people in this small community?
The path shouldn't contain the executable itself - just the directory containing java.exe. So you want this on your path:
C:\Program Files (x86)\Java\jre7\bin
Restart your console, check that the path is correct (just run path and look at the output) and all should be well.
Note that if you're going to be developing Java code, you should probably use the JDK path instead of the JRE path. For example, my path contains c:\Program Files\Java\jdk1.7.0_17\bin. (Yes, I need to update :)
I hope you do not have changed your PATH variable, only added (...);C:\Program Files (x86)\Java\jre7\bin\ (on UNIX systems, use a colon (:) as the separator instead) at the end. Otherwise, you will have many issues with all other Windows applications. Note that you must add the folder that contains java.exe, not the path to the executable itself.
Then, try to restart your Command-Line or even your Windows session.
Optional: You may set JAVA_HOME to specify JDK location. See this link.
Here you have a tutorial of oracle for setting the path
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
C:\Program Files (x86)\Java\jre7\bin --> This would be your PATH variable.
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 .
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.