Java - Command line to run a class with another jvm - java

I've java 1.8.0_131 by default on my machine but also java 9, so can I run a compiled class with jdk-9? is it possible anything like this:
java -v path/to/jdk-9 myClass
Thanks

The java command actually invokes the JVM. That JVM starts up, and reads your classes, and runs what you tell it to run.
There is no way to tell the "starting" JVM to actually use a different JVM. java isn't some sort of wrapper that "later" invokes some binary. It is already the binary that starts the JVM.
In other words: what you are asking for isn't possible. A simple workaround can be to define "aliases" on the command line, like java-8 to start your java8 installation, and java-9 to point to that other installation.

By default java command call the installed java version which can be checked by command:
One way could be update the java:
sudo update-alternatives --config java.
O/P of above command:
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode
Press <enter> to keep the current choice[*], or type selection number:
In your case java 9 will also come. update java version and run java command.
Or you can do directly call your java9 script which you can find ${Java9 Installation Directory}/jre/bin/java -v class.
PS: Above description of commands is based on ubuntu.

Related

Java command not working and keep on running for infinite

I am trying to execute the java command but every commands its goes for infinite loop.
for example
1.java -version
2.java -help
above both commands keep on running and never returns a result this problem occurs from today morning for every java commands ex : jps,jconsole etc.
->JAVA_HOME and PATH variable already defined.
which java
~/Java/jdk1.8.0_102/bin/java
and
echo $JAVA_HOME
/home/arc/Java/jdk1.8.0_102
and
file /home/arc/Java/jdk1.8.0_102/bin/java
/home/arc/Java/jdk1.8.0_102/bin/java: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
Problem is with GLIBC-2.12, recently I installed GLIBC-2.14 manually ,problem arise due to conflicting between them.

Running programs depending on java not working

I have several programs running on Java that suddenly stopped working.
I ran the commands to start them at command line and nothing happens. When I ran: $java -version nothing happens either.
If a I run: whereis java the output is:
java: /usr/bin/java /etc/java /usr/bin/X11/java /usr/share/java /usr/share/man/man1/java.1.gz
And if I ran wheris {the program depending on java}, I get a similar output. For example, for fastqc software:
$whereis fastqc
fastqc: /usr/bin/fastqc /usr/bin/X11/fastqc /usr/share/fastqc /usr/share/man/man1/fastqc.1.gz
I have no idea what to do. Besides, I checked if java is properly installed by:
apt-get install default-jre
And the output was:
Reading package lists... Done
Building dependency tree
Reading state information... Done
default-jre is already the newest version.
The following packages were automatically installed and are no longer required:
ifeffit libbit-vector-perl libcarp-clan-perl libchemistry-elements-perl
libchemistry-formula-perl libconfig-inifiles-perl libdate-calc-perl
libdate-calc-xs-perl libdate-manip-perl libifeffit-perl libio-stringy-perl
libmath-combinatorics-perl libmath-derivative-perl libmath-round-perl
libmath-spline-perl libntdb1 libole-storage-lite-perl
libparse-recdescent-perl libreadonly-perl libreadonly-xs-perl
libregexp-common-perl libspreadsheet-writeexcel-perl
libstatistics-descriptive-perl libterm-readkey-perl libtext-english-perl
libtext-glob-perl libtie-ixhash-perl libtime-stopwatch-perl
libtk-filedialog-perl libtk-gbarr-perl libtk-histentry-perl libtk-pod-perl
libtk-splashscreen-perl libwwwbrowser-perl libxray-absorption-perl
libxray-scattering-perl linux-image-3.19.0-25-generic
linux-image-extra-3.19.0-25-generic perlindex pgplot5 python-ntdb
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
I also made update, remove and re-install; but nothing changes.
If you have any suggestions please... I am obviously NOT an advanced-informatician...so I do not know what to do.
Thanks!
Have you tried to restart? Usually a nice go to as a first pass at debugging.
Did you not accidentally rewrite your $PATH? i.e. java is still installed, but not in your default path anymore. So running java --version will also fail.
You could also try sudo apt-get install openjdk-7-jdk to install the development kit for java. This also contains jre so might fix things. But my first quess would be your $PATH is broken.
It looks that the problem was the two versions of java that I have installed. The programs I was trying to run needed an older version of java, and when I installed the newer it has been set to use it as default. I finally solved this out, changing the default version of jave to use:
update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1074 auto mode
1 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode
2 /usr/lib/jvm/java-8-oracle/jre/bin/java 1074 manual mode
Press enter to keep the current choice[*], or type selection number: 1
And that's it!
:)

Why Linux java -version by ssh give different result?

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.

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");

Using Bumblee (Graphics Drivers) with Eclipse IDE [duplicate]

Does anyone know how to make eclipse or netbeans use the graphics card in optimus laptops by invoking optirun (bumblebee) inside the IDE so that one can just use the run button in the IDE to run the program in a graphics card within the IDE.
In simplest form I just want the IDE to do the equivalent of optirun ./javaproject
The way I did this in Eclipse was to first start the Java debugger jdwp and listen to a port. Then start the JVM with optirun java ... and use jdwp to connect to this port. Both tasks can be started at the same time in Eclipse by creating a Launch Group in the debug configuration settings (Run -> Debug Configurations). In detail:
Create a Remote Java Application debug configuration with "Standard (Socket Listen)" Connection Type and some arbitrary port, e.g. 56789. This attaches the Java debugger jdwp on port 56789 to a virtual machine which accepts debug connections at this port.
Now we need to start a JVM with optirun. This can be done with a External Tool Configuration (Run -> External Tools -> External Tool Configurations). Create a new Program configuration in the left side of the External Tools Configurations window. You could directly start optirun java <additional arguments> by filling in the required fields. However, I have decided to use a shell script which is reusable by different projects (As can be seen below, there is one part missing to make it entirely reusable. I'm glad for any help from more experienced Eclipse users...). Hence, the Location field points to this shell script. The script itself accepts three arguments: the classpath for the project, the name of the Java executable, and the port number. These arguments can be passed to the script in the Arguments field of the Main tab, e.g.
${project_classpath:${selected_resource_name}}
ExecName
56789
The shell script looks like this, assuming optirun is in your PATH:
#!/bin/sh
CLASS_PATH=${1}
JAVA_EXECUTABLE=${2}
PORT=${3}
# TODO: fix this java library path: pass it as an argument as well. Is there an Eclipse variable which stores this?
JAVA_LIBRARY_PATH=/usr/local/share/OpenCV/java
#------------------------------------------------------------------------------
optirun ${JAVA_BIN} -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:${PORT} -Djava.library.path=${JAVA_LIBRARY_PATH} -Dfile.encoding=UTF-8 -classpath ${CLASS_PATH} ${JAVA_EXECUTABLE}
#------------------------------------------------------------------------------
Finally, the two pieces are brought together in a Launch Group in the Debug Configurations window (Run -> Debug Configurations). Create a new Launch Group and add the two previously generated Debug configurations by clicking on Add in the Launches tab and by selecting the appropriate configurations.
Note that due to the classpath variable in step 2 (i.e. ${project_classpath:${selected_resource_name}}), the appropriate package needs to be selected in the Package Explorer before clicking on the run debug configuration button (make sure that the Launch Group is selected).
This solution works perfectly for me: I can debug Java code inside Eclipse which calls native code involving CUDA optimizations and Bumblebee only activates the discrete graphics card when necessary.
Just use optirun to start the IDE. For example, optirun eclipse or optirun netbeans
I build the project in Netbeans (F11) and run the following in a terminal:
optirun java -jar path/to/javaproject/dist/javaproject.jar
Mind that if you have any java parameters in your project, you need to add it manually. My workflow is like this:
Locate the Java options from the project, open Project -> Properties, Run. At VM Options I see -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux. I also have some parameters that I want to pass to main(String[]). With this information, I open a terminal and run:
cd path/to/javaproject
optirun java -Djava.library.path=lwjgl/native/windows;:lwjgl/native/linux \
-jar dist/javaproject.jar some paremeters
Another hint, if you have to open and close the program frequently, run optirun bash in a different tab so that preparing the use of the graphics card becomes faster. Alternatively, you can run optirun netbeans, but that means that the nvidia card will always be on even if you are programming which increases power use and increase the heat.
Important: if you are using a 32-bit JVM or Java libraries on a 64-bit machine, you also need to install the 32-bit drivers and libraries. For Ubuntu, the nvidia package already contains 32-bit drivers, see this answer. For other distros, you likely need to install lib32-* packages for Mesa, VirtualGL and nvidia-utils.
You can also rename java to java_real and use this portion of code as your java command :
#!/bin/bash
path=$(dirname $(readlink -f $0))
args=""
runner="$path/java_real"
for var in "$#"
do
if [ "$var" = "-3d" ]; then
runner="primusrun $runner"
else
args="$args $var"
fi
done
$runner $args
NOTE : I had to do this in /usr/lib/jvm/java-7-openjdk-amd64/jre/bin, not in /usr/bin to make it work with Eclipse.
In Eclipse, just add "-3d" in your program arguments and you're good to go !

Categories

Resources