GCJ and Java Home - java

I am trying to install a software on CentOS and the doc says something like this...
If you are using a Linux distribution
that installs GCJ by default (which
includes all of the most popular
distros), then before you begin
installation you must remove, disable,
or circumvent GCJ. If you cannot
remove it, you can simply ensure that
your JAVA_HOME variable is properly
set, and add the Java Runtime
Environment's /bin/ directory to the
beginning of your PATH variable in
~/.bashrc or /etc/environment, then
relog before continuing.
I will like to know the exact commands I need to execute to that effect.
(Java Newbie)

After you've installed for example the Oracle JDK then you can use the following command:
alternatives --config java. This command has to be performed as root user.

Related

Confused about what jdk and jre my command prompt is using for java

I was super confused about compatibility on a java tool I was using and ended up downloading different versions of java so now I do not know what versions of jdk or jre I am actually using. When I compile I use -- release 8 in my command prompt, what does that exactly mean?
Currently my programs folder has jdk-18 in the java folder
and my program files(x86) folder has
jdk1.6.0_45
jdk 1.7.0_80
jre 1.8.0_ 321
jre6
jre7
My java home environment variable is C:\Program Files\Java\jdk-18
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
when I type java -version in my command prompt I get
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
So does that mean my cmd uses jre 1.8.0_ 321? What about the jdk? How do I change it if I need to?
When you run java -version - first java found in PATH environment variable is used. To be sure use commands where java (Windows) or which java (Linux) - it will tell you the location.
When you run mvn install - JDK found in JAVA_HOME environment variable is used. To be sure use mvn -version - it will tell you the location.
When you run from IDE - IDE settings matter, usually you specify JDK per project.
When you run javac --release 8 Something.java you are asking compiler to produce output compatible with the version you specified - it has nothing to do with JDK you are actually running it on. This flag was added in JDK 9, so if it doesn't fail for you then it means you are running on JDK>=9.
If you want to be 100% sure just fully qualify the path - for example on Windows "C:\Program Files\Java\jdk1.8.0_152\bin\javac.exe" Something.java
So does that mean my cmd uses jre 1.8.0_ 321?
Yes.
What about the jdk?
Unclear.
Run javac -version and that will tell you what version compiler you will run by default.
The versions of the Java tools that you get when you type java, javac, jar (etcetera) at the CMD prompt are solely determined by the PATH environment variable setting in effect at the command prompt.
The other variables do other things ... but not this.
How do I change it if I need to?
If you want to change what typing java does, change PATH.
Note that the PATH variable is a standard Windows environment variable that affects all* commands, not just the Java tools. There should be lots of tutorials on how to set PATH, and on the Windows CMD prompt in general. You should probably take the time to read them.
You can also just use the absolute path for the Java tools; e.g. "C:\Program Files (x86)\Java\jdk1.7.0_80\bin\java.exe" is probably the correct path for the Java 7 java command. (You can easily check ... and find the correct path by looking at the installation tree.)
Regarding your other variables:
My java home environment variable is C:\Program Files\Java\jdk-18
That is presumably JAVA_HOME. That tells (many) 3rd-party tools which Java tools to use. But unless you have configured PATH to depend on JAVA_HOME, it won't alter what typing java does.
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
That is presumably CLASSPATH. That variable provides a default path that java will use to find classes to load. For example, if you run java com.acme.Example, it will use CLASSPATH to search for the compiled Example class.
So "C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar" is almost certainly incorrect. (That JAR file contains the classes that implement various Java tools. It doesn't need to be on the classpath, unless your Java application is running one of those tools. And even then there would need to be other things on the classpath for your application to work.
You need to do some reading on what the Classpath is, how it works, and how to set it correctly. Setting it to stuff randomly is a sure fire way to make your Java code not work ...

Setting Environment variable in Linux

I am using Maven for my project and I need to set environment variables in my system for it to work. I have set environment variable for maven but it also requires setting up Java environment variable. I am accessing linux through Putty through a remote server. I tried setting up JAVA_HOME environment variable but I get this error:
Error: JAVA_HOME is not defined correctly.We cannot execute /home/z222189/jdk1.8.0_31/jre/bin/java
I used export JAVA_HOME=/home/z222189/jdk1.8.0_31/jre for this purpose
Even when i try to execute the java.exe file directlty in linux I get error
-bash: ./java.exe: cannot execute binary file
Any suggestions on how to resolve this issue?
You shouldn't refer to the jre/ subfolder, as Maven will need a Java compiler and potentially other JDK-only tools.
So just use:
JAVA_HOME=/home/z222189/jdk1.8.0_31
EDIT: (making sure that a correct, working JDK is installed at that location, of course...)
Seems you are in Linux, be sure you get Java for Linux!
Set JAVA_HOME using
export JAVA_HOME=/home/z222189/jdk1.8.0_31
Does not work because java is not there, or the version is not the correct, if you already installed correct java linux version and you dont really know where your oracle-java is installed, you can find this running
which java
which will tell you which binary of java is being called, normally is something like
/usr/bin/java
but you can
readlink -f /usr/bin/java
that will give you something like
/usr/lib/jvm/java-7-oracle/jre/bin/java
ergo
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/

Run a Java program with java.exe in the same directory without installing

I know there are lots of posts similar to this, but all of their answers require creating an .exe file. I want to know how to run a program in a folder without installing java.
I don't want to install Java 8 as it breaks some of my programs. I want to run a java program in a folder using a start.bat file that points to the java.exe file in the same directory. Like this:
"java.exe" -jar program.jar
The error is like this:
Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
has value '1.7', but '1.8' is required.
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.
Since the command window is opened in the same directory, "java.exe" should point to the java runtime.
Can I maybe copy java.dll to the directory to make it work? How can I do this without installing java 8? Could I use java ME?
Why don't you create a .bat file that declares a new JAVA_HOME and appends to the PATH just for this DOS/batch session, then you can point to a place where you have the required JRE (however, you kind of have to install it... it's a Windows thing).
I understand you are reluctant to install the JDK/JRE 8 on your Windows because of this: "I don't want to install Java 8 as it breaks some of my programs". But trust me, you can always change the JRE version on the Java Control Panel (windows >> control panel >> Java) and change the JAVA_HOME & PATH environment variables, you might have problems with your browser's plugins though, the Java install will affect them as well.. I guess, in that, case you can either try to skip this part of the install process or.. install JDK 7 again afterwards?
IF you just want to run this program, you can also install VirtualBox and create a VM with windows and install the JDK8 there, avoiding any problems in your OS.

What is the difference between the JAVA_HOME environment variable and the output of "java -version"

What is the JAVA_HOME environment variable actually? I'm in doubt. What is done by setting it ? And I have also another question that if we have two or more JDK s in the OS, we can set one version's path to set the $JAVA_HOME variable and another version as the running java in the machine which is shown by "java -version" command.As I think in ubuntu, java --version command gives the JRE configured (running java) version of java and JAVA_HOME variable defines which version to use for compiling java programs. Am I correct? What is actually the difference between the jdks referred by these two outputs-
1. echo $JAVA_HOME
2. java -version
Lets start with what the two commands do.
echo $JAVA_HOME
This outputs the value of the JAVA_HOME environment variable. By convention, this environment variable is set (typically in a shell "rc" file) to the base directory of a JRE or JDK installation. However, none of the Oracle or OpenJDK core java tools depend on this. You set JAVA_HOME for the benefit of 3rd-party tools, to tell them which of possibly many JRE or JDK installations on the system to use.
So running echo $JAVA_HOME will typically give the directory for a JRE or JDK. But it might be unset ... or point to missing directory.
java -version
This runs the java command and tells it to output its own version information. Unlike the previous, if your shell can find a (real) java command, the information it outputs is definitive.
But which JRE / JDK does will the java command come from?
Well that is determined by the shell, and how it (in general) finds commands. For all mainstream Unix / Linux / BSD shells, this is controlled by the PATH environment variable. The PATH variable defines a command search path; i.e. a list of directories where the shell looks for commands.
So if PATH is /usr/bin:/bin:/usr/local/bin:/home/joeuser/bin:/usr/java/jre-x.y.z/bin then the shell will look for java in each of those directories in turn. The first one java command that it finds will be the one that is executed. You can find out which one it is by running this command.
which java
But that is not the end of it, because if you are using an OpenJDK Java installation on a typical Linux system, the java command in /usr/bin is actually the first in a chain of symlinks that is managed by the alternatives command. The actual executable is at the other end of the chain.
(The alternatives system is a way to implement a switchable system-wide default for a command or set of commands. Read the manual entry for more details. But note that Oracle Java installers (or RPMs) for Linux don't "grok" the Linux alternatives system ... one of my bugbears!)
Finally, it is common to see something like this in a shell "rc" file:
export JAVA_HOME=/path/to/jre # (or jdk)
export PATH=/bin:$JAVA_HOME/bin:/usr/bin:...
There is nothing magical about this. The $JAVA_HOME is going to be expanded by the shell before it sets the environment variable. Tthe actual PATH value will be:
/bin:/path/to/jre/bin:/usr/bin:...
So in summary:
echo $JAVA_HOME tells you the JRE or JDK that a typical 3rd-party tool will try to use by default.
java -version tells you the JRE or JDK that the java command comes from.
They may be different.
JAVA_HOME is an environment variable that contains the directory where Java is installed (if a few versions of Java are installed - this will point to the default)
java -version is a command that shows which version of Java is installed (and again, the one that's used by default in case a few versions are installed)
Addition: Thanks to Luiggi's comment: if the folder where java is installed is not in the PATH (another environment variable) - running java -version or any other java command will fail!
Its not related to jdk and JRE. java -version it is used by the java which is in /usr/bin/java. In some application is used $JDK_HOME. Both the jdks are same. Or we can define different jdk.
echo $JAVA_HOME
will print the value of environment variable called JAVA_HOME. If it's not set, it will print an empty message. Usually, you set JAVA_HOME to the path of the latest JDK installed in your pc.
java -version
will print the version of the java executable (Java Virtual Machine) that is set in your path, if configured. Otherwise, it will throw an exception on the command line.

Even if $JRE_HOME is not set java -version is displaying properly

I used sudo apt-get open-jdk7 to install Java on my Ubuntu OS.
When I open a terminal and type echo $JRE_HOME it displays nothing. But then I do java -version it shows me correct version of OpenJDK. Also when I simply type java it works.
Not sure how Ubuntu set the path of java.
Similarly javac works but I don't see anything when I say echo $JAVA_HOME.
My path does not show java directory path in it.
Where is Ubuntu setting the java path? Why does Ubuntu not set JAVA_HOME and JRE_HOME during installation of OpenJDK?
It's a common misconception that Java somehow requires JAVA_HOME, JRE_HOME or similar environment variables to be set to work.
In fact the java and javac (and related) executables don't really care about those variables. They only need to be on the PATH (or can be executed via a full path if not on PATH) and will find their related install by looking "near itself" (i.e. the executable are usually installed in a known location relative to the JVM files).
There are a few third party scripts that use JAVA_HOME and/or JRE_HOME to find a Java installation (Apache Tomcat is a well-known example for this), but the Java executables themselves don't need those variables.
As to how Ubuntu finds the executables is easy to answer: use which java and which javac to find out which executables are executed when you type those commands. On Debian-based systems they are usually a series of symlinks through the alternatives systems that point to the actual executables.
"Where is ubuntu setting java path?", ubuntu does not set the open-jdk-java-path automatically.
try
find / -name "java"
to locate the javac. And write
export JRE_HOME="xxx"
(where xxx is the base of the open-jdk-jre.)
BTW: You got nothing than trouble with open-jdk. You better use the sun-jdk.
As many Linux distros, Ubuntu has /etc/alternatives in its paths. If you type:
which java
Then you'll get
/etc/alternatives/java
If you type:
ls -l /etc/alternatives/java
Then you'll get
/etc/alternatives/java -> /usr/lib/jvm/<your java version>
A few mor ls -l -s can help you to get the full picture.

Categories