compilation of java program - java

I have JRE 6 in my PC (Windows XP) but when I compile a program in the command prompt it shows javac is not recognised as internal or external command. Which software is needed to compile a Java program?

You need to download and install a JDK, not a JRE.
As a (very rough) explanation, the JRE contains just the Java Virtual Machine, whereas the JDK contains not only the JRE but also the compiler (javac), some debugging tools (javap, jvisualvm, ...), extra libraries and the API documentation.
See also:
What is the difference between JRE and JDK?
as recommended by dogbane, you can follow the Java Hello World for Windows Tutorial.

I suggest you run through the steps of the Hello World! Tutorial.
As per the instructions:
Download JDK6. (Make sure you download the JDK, not the JRE.)
Update the PATH variable to be able to conveniently run the JDK executables such as javac from any directory without having to type the full path of the command

JRE stands for Java Runtime Environment. It allows you to run already compiled java programs. To compile your own programs, you need JDK which stands for Java Development Kit. You can download it at JDK Downloads Page.

You need to install JDK-6 (Java Development Kit) which contains javac Java compiler. JRE does not contain it.

Instead of a simple JRE, you'll need a full JDK. As an example, youc an download sun one here : http://www.oracle.com/technetwork/java/javase/downloads/index.html#need (and don't forget to select "download JDK").

JDK is needed to develop applications whereas JRE is enough to run applications.
In other words JRE is enough to run a application (i.e) it includes java.exe and doesnot contain javac.exe
JDK contains both java.exe and javac.exe
There are also other chances that you dint set your class path properly.

Related

JDK java executable vs JRE executable

I installed the Oracle JDK to /usr/jdk/jdk1.8.0_25 and set that directory to JAVA_HOME.
JAVA_HOME/bin contains the java executable and JAVA_HOME/jre/bin contains another java executable.
The guide I followed said to add JAVA_HOME/bin and JAVA_HOME/jre/bin to the PATH but which is the correct executable to use and which will be used if I call java from the command line?
Here is a simplified overview of the differences between JDK and JRE
JRE means JavaRuntimeEnvironment : it contains only binaries for running java program
JDK means JavaDevelopmentKit : it contains binaries for running java program + binaries to compile java source code (and produce a java program).
A JDK always contains a JRE inside (under directory <JDK_HOME>/jre)
The major difference between JRE and JDK is the javac program. (javac means java compiler)
(you will also find some other programs under the <JDK_HOME>/bin that aren't present under <JDK_HOME>/jre/bin : all of them may be useful to do java development - but in most case - useless to run a java program.
All programs that are in both locations (i.e. <JDK_HOME>/bin and <JDK_HOME>/jre/bin) are the same, so it is not very important to make a distinction.
To answer precisely to your question : the java instance that will run when you execute a command line starting with java is the first instance of java found in your PATH system variable... just like any other program.
Whatever is earlier in the path for java - JAVA_HOME/bin or JAVA_HOME/jre/bin.
If you take a look at the folder structure where jdk / jre is installed you would see that in the jdk/bin folder there are quite a collection of binaries such as java, javac, javap etc. The JRE would include java but not javac, javap etc. I am assuming this is linux and you downloaded the compressed archive and extracted that.
If you are really curious, you can write this to the command line:
where java
This will give the exact location of the executable. On my PC this gives me
C:\ProgramData\Oracle\Java\javapath\java.exe
which is a soft link for the system default runtime java.exe (in my case it is C:\Program files\Java\jre 1.8.0_25\bin\java.exe)

Mac OSX. Java 7 or 1.7, which is the current version?

I am trying to run an application (on MacOSX) that relies on Java. Docs are telling me that I need to update from Java 1.6 to 1.7. I then found, downloaded and installed java 7 from Java.com, but I'm still getting java version error when running app. when I enter 'java -version' into terminal it tells me I am still running 1.6?
I'm sure there is a logical explanation for this, but at the moment I feel Like I am in the twilight zone. What is the current version, 1.7 or 7? Why is it that in System Preferences it says I have Java 7 installed, but 'java -version' says I have 1.6?
Help!
I just have this in my bash profile:
export JAVA_HOME=$(/usr/libexec/java_home)
... This should ensure that your Mac references Java 7 where applicable. Per the Apple docs:
The /usr/libexec/java_home tool dynamically finds the top Java version
specified in Java Preferences for the current user. This path allows
access to the bin subdirectory where command line tools such as java,
javac, etc. exist as on other platforms. The tool
/usr/libexec/java_home allows you to specify a particular CPU
architecture and Java platform version when locating a $JAVA_HOME.
(From Technical Q&A QA1170: Important Java Directories on Mac OS X)
EDIT re bash profiles:
If you don't know about shells and their configuration, you have a little work ahead ofyou. However, Nathaniel Landau's post, My Mac OSX bash profile may well help you.

Confusion about Java Versions

So I'm trying to get the latest version of Java. When I run:
java -version
I get:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
When I run:
javac -version
I get:
javac 1.6.0_65
Now I've just downloaded and installed JDK 8. When I go into System Preferences --> Java --> Update, I see:
Your system has the recommended vesion of Java.
Java 8 Update 05.
I guess I have a few questions:
1) Don't I want the JDK and my version of Java to match up?
2) Why does my Java Control Panel claim I have Java 8, but my work in the terminal (when checking my Java version) says otherwise?
Thanks for the help,
Mariogs
The probably "simple" answer is you have two versions of java installed. On the command line you currently use 1.6. Thus the old one is active. In system preferences you see the version of java 8.
Windows:
You can change the version of the command line to java 8. Set PATH environment variable and JAVA_HOME or via windows preferences. I recommend the first one.
Mac: /usr/libexec/java_home is the starting point for switching java versions on the command line. Check out this post to understand how to handle different java versions on the Mac. IMHO this answer is a good solution.
The Java Platform offers both the JRE and the JDK in order for users to run Java programs. The JRE stands for the Java Runtime Environment, and the JDK stands for the Java Development Kit.
The JDK is meant for Java developers - that is, those who build applications/write programs in Java. It contains tools that are needed for Java coding, including -javac to compile programs.
The JRE is meant for regular users - those who only need to run Java programs on their computer and are not interested in development.
The reason for the discrepancy in your case is because you're looking at the JRE and JDK and trying to compare the two. The current JDK that you have is Java 8, whereas the current JRE that you have is 1.6.0_65. It is problematic that your JRE version does not match your JDK version, but without your PATH variable or other information about your install, we can't help you fix your installation.
1) Yes, if you use the JDK at all, you want the JRE (runtime environment) to come from the JDK (development environment) (a JDK necessarily includes a JRE).
2) Likely your path variable is set so that you invoke Java from your Java 6 installation; you need to find the equivalent for your Java 8 installation and set the path for that. Without information about your operating system, we can't help you do that.
We should know the reason for this
Our OS comes with a predefined (built-in)set of tools and utilities. When we try to execute the command e.g. cls in the Windows command line then it is already present in system path variable and os will refer the corresponding binary of cls to execute the command.
However, when we install any third party tool/software then path variable is not updated accordingly.
When we install different versions of java on your system then installations go to different directories. E.g. JDK installation directory for Windows will be
C:\Program Files\Java\jdk1.8.0_161
Similarly, JRE installation directory for Windows will be JDK installation directory for Windows will be
C:\Program Files\Java\jre1.8.0_161
We need to update the path variable of OS to point to the appropriate directory. If we set the path of JDK then it will execute a binary from JDK bin directory.
Solution
we need to update JDK or JRE version specific directory location into PATH Environment variable.
Let me see if I can clear it up for you.
1)Yes, arguably you nearly want this to be true.
2)It could be few things, but most likely that a previous instillation was not properly removed. So one gets called instead of the other.

ADT Bundle can't find Java

When I open adt-bundle-windows-x86_64-20131030 and click on the Eclipse.exe application to install it I get the message; A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. No Java Virtual Machine was found after searching the following locations: C:\users\username\desktop\development\adt-bundle-windows-x86_64-20131030\eclipse\jre\bin\javaw.exe
javaw.exe is your current PATH.
I do have JDK, and JRE on this computer, I need them to view the emulator for my job. I have tried uninstalling and reinstalling Java with the same results. What can I do to fix this problem?
You will have to put Java in your classpath, so Eclipse knows where it can find Java. See here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html for info on how to do this.
install 32 bit version of jdk from oracle website
put the path of your java bin directory under Environment variables.
System->Advanced System Settings->Environment Variables->path
Open command prompt. use java and javac commands to know if you have successfully installed java and compiler.
if it doesn't help, this question has a lot of helpful answers
Eclipse - no Java (JRE) / (JDK) ... no virtual machine
From what you said:
"'java' is not recognized as an internal or external command, operable program or batch file"
Your java may not have been installed correctly, or, it was installed, but you did not set path to it correctly. It is not a problem with ADT, any of your programs that need Java will not be able to find it.

Installing JDK and setting JAVA_HOME

What exactly is the purpose of JDK - running java programs or developing?
There seems to be too many packages that include java and jdk/sdk in their title. Which one should I install if I want to develop Java applications?
Who uses JAVA_HOME and what should I set it to be?
JDK is development environment. JVM is runtime environment.
You should download java installer from oracle web site or install it from repositiry manager
JAVA_HOME system variable usually is required for enterprice java software (application servers, DBs, development tools and so on). You should set JAVA_HOME variable to root directory of JDK or JVM(setting it to JDK always works, but maybe some software will run as expected with JAVA_HOME pointing to JRE). If you set JAVA_HOME right then $JAVA_HOME/bin/java -version command should pring version of java.
JRE means Java Runtime Environment. Most Java Software only needs a JRE to run.
JDK means Java Development Kit. A JDK allways includes a JRE. If you develop you most certainly allways use the JDK, since it contains source and docs of java's own Classes.
Also there is software (usually software for development) that uses tools of the JDK to run, e.g. this software needs the JDK as runtime environment (for example: ant).
JVM means Java Virtual Machine and is the program that runs your java program no matter if in JRE or JDK.
JAVA_HOME is the path to the JDK
JRE_HOME is the path to the JRE
JDK - Java Development Kit (in contrast to JRE - Java Runtime Environment).
If you don't know which version of Java you need, just get the latest (currently Java Platform (JDK) 7u5). Unless you already have a development tool, you might want the Netbeans bundle instead.
JAVA_HOME is used by some software that depends on Java - ant, which is a build tool, is one that comes to mind. It should be set to the directory which contains the bin directory, which in turn contains the java executables.

Categories