Why Linux java -version by ssh give different result? - java

So my problem is why when I execute "java -version" command as root in my cent os it gives me "java runtime 1.7". But when I use ssh to ssh the same root user and execute "java -version", I got "java JDK 1.6" and this problem causing my deploy activity not working. So anyone know, why I got different result when use ssh command and not.
Thanks all...

The problem is due to setting of $PATH being different in the two contexts.
Run echo $PATH and which java in both contexts and compare the results.
Irrespective of why the $PATH settings are different, one solution is to use the absolute pathname for the java binary that you want.
It is also worth noting that both Java 6 and Java 7 are passed EOL, and you should really be upgrading your applications to use Java 8. Especially if you are using Java to run network-exposed services ... or (>>shudder<<) in a web browser.

Chances are your root login has a different PATH environment variable setting than your ssh login shell.
which java will probably (depending which shells you're using) tell you which java binary is first in your PATH.

Related

Do I need to set JAVA_HOME on MacOS?

I'm fairly new to Java. Many of the tutorials on installing Java on a Mac recommend setting up the JAVA_HOME environment variable. However, Java works fine on my computer with the JAVA_HOME variable being empty!
I have successfully used Java from the command line (using javac and java), in IntelliJ IDEA (there in the "Project Structure Settings" I've set up the "JDK home path" to /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home) and in Sublime Text to compile and run Java files.
So, my questions are:
Why does Java work without the JAVA_HOME on my Mac?
Do I need to set up JAVA_HOME at all? What would be the consequences of not having it? Maybe it is not essential for some things, but is required for other things to work? Then what are these things?
Is this behavior different on other operating systems?
Outputs of some commands are:
echo $JAVA_HOME outputs an empty line
echo $CPPFLAGS outputs an empty line
which java
/usr/bin/java
echo $PATH contains /usr/bin
Why does Java work without the JAVA_HOME on my Mac?
Setting JAVA_HOME is not required for Java to work in your system. Some applications (e.g. Tomcat, Maven etc.) however look for JAVA_HOME system variable and if its value is not set, they may prompt you to do so.
Do I need to set up JAVA_HOME at all? What would be the consequences
of not having it? Maybe it is not essential for some things, but is
required for other things to work? Then what are these things?
Already answered above.
Is this behavior different on other operating systems?
It's same across all operating systems.
It depends on the program.
Most programs that depend on Java follow logic like this
if $JAVA_HOME ; then
use JAVA_HOME/bin/java
elif $(which java); then
use java command on PATH
else
error
fi
That being said, to be consistent, you should set it , and using SDKman or Homebrew does that for you

How to execute Carrot2 Document Clustering server

I downloaded the Carrot2 Document clustering server build 3.15.0 for Mac. The read me file says:
The DCS requires a Java Runtime Environment (JRE) version 1.7.0 or later. To
run the DCS, execute the 'dcs' script and point your browser at
http://localhost:8080 for further instructions.
Mac OS Sierra doesn't make it easy, but I got 1.8.0_112 installed.
The problem is that I don't know how to execute the 'dcs' script.
There are .cmd, .sh, .war, and .jar files. I wasn't sure which of those to work with. I thought .jar looked promising, so I followed some of this thread and tried this in a terminal window:
java -jar invoker.jar
I cd-ed to the correct directory, but it just says Provide main class, but I'm not sure what or where that is.
Can anybody provide instructions or a link to how to do this?
Use the dcs.sh (on Linux/Mac) and dcs.cmd (on Windows) to start the server. The scripts will set some extra options for the JVM and then start the DCS. In case of any problems, append the -v option to see diagnostic output.

Use specific jvm installation when call java application from php code

I have two installations of JDK on my account and I do not have root access to change the default one which is "usr/bin/java" to specific one /home/username/jdk/bin/java"
I tried to do the following in my php code:
$JAVA_HOME = "/home/username/jdk";
$PATH = "$JAVA_HOME/bin:".getenv('PATH');
putenv("JAVA_HOME=$JAVA_HOME");
But when I execute the following just below above mentioned lines:
which java
I got /usr/bin/java
Any idea please how to make it works? The java application I want to run from php code does not work on the default one.
Thanks
If feasible, qualify the java command, e.g. /home/username/jdk/bin/java, when running your Java program.
This way you can even use multiple different JVM's in different places of your PHP code.
Note: I know that using qualified access like this works on Windows, even if JAVA_HOME points to a different installation. I'm assuming the same is true for Linux.
Try to set JAVA_HOME as temporary parameter for the context of your application, i.e. right before the command (see the second command, the others just show that the variable is only set for that one command):
> java -version
java version "1.8.0"
> JAVA_HOME=~/jdk1.7.0_51.jdk/ java -version
java version "1.7.0_51"
> java -version
java version "1.8.0"
In PHP:
exec("JAVA_HOME=~/jdk1.7.0_51.jdk/ java yourapplication");
Source: Bash Reference Manual – Environment
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
To see the differences in a browser I tested with this ugly PHP snippet:
<?php
echo "<pre>" . shell_exec('java -version 2>&1') . "</pre>";
echo "<pre>" . shell_exec('JAVA_HOME=~/jdk1.7.0_51.jdk java -version 2>&1') . "</pre>";
echo "<pre>" . shell_exec('~/jdk1.7.0_51.jdk/bin/java -version 2>&1') . "</pre>";
?>
You may also use a specific version of the java executable by explicitly using it to run your app:
shell_exec("~/jdk1.7.0_51.jdk/bin/java -version");

CMake can't find Java because of Cygwin

A user of my software is running Windows with Cygwin installed, which provides access to the standard GNU which command. In my CMakeLists.txt there is a line that reads:
FIND_PACKAGE( JAVA REQUIRED CONFIG )
This fails on his machine, because both JAVAConfig.cmake and java-config.cmake (executed by FIND_PACKAGE) use this command as a first-line attack in locating Java. The command returns a bad path to a non-existent install of Java. What can I do to override this annoying behavior from my CMakeLists.txt?
Ask him to send you his "path" env. variable, you will find something interesting there for sure.
Other issue is Java installations.
Ask him to unstall old javas or just update to newest, current installer removes older copies.

jnetpcap library unable to build on ubuntu 12.04

I am trying to install JNetPcap and followed the instructions given at here. At step 12, I am unable to run the ant command and i see the error
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-6-sun/bin/java
As I am able to run Java classes from eclipse or from command line I don't think if it's a problem with JAVA_HOME.
echo $PATH shows
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/java-6-sun/bin
Kindly let me know if am missing something here.
thanks in advance
Neither running java from the command line or running eclipse will require JAVA_HOME to be set. However, the build procedure you are trying to use ant, and ant often does require JAVA_HOME to be set appropriately. (It actually depends on the version of ant that you are using. The use of JAVA_HOME is typically in the wrapper script for ant.)
Just set it.
JAVA_HOME should probably be set to /usr/lib/jvm/java-6-sun ... based on what you gave said PATH to.
However, it is also possible that the problem is that your PATH is incorrect. Or that you have (somehow) managed to get the owner/group/permissions on your Java install incorrect, such that the java command isn't executable.
Check that running java -version displays the installed Java version.
Repeat with /usr/lib/jvm/java-6-sun/bin/java -version.

Categories

Resources