I need to get a stack trace for a JVM process running on a client machine that uses windows.
The client has the JRE installed but not the JDK.
I want to use JStack but it is not installed and we can't install a JDK on the client's machine. I also tried using AdaptJ stack trace product from a Java Webstart Session but that didn't work because we remote in and get an error about not being the session that started the application at a specified PID.
Essentially I want a way to install JStack without installing the JDK.
You probably want to use SendSignal, which was designed for exactly this purpose.
The JDK and associated tools work fine whether "installed" or not, if you just zip up and extract it to a temporary directory, you should be able to run jstack. (No PATH or JAVA_HOME modifications necessary). Just make sure you use the same version that corresponds to the JRE your client has the application running with. At least in the case of JConsole, it seems to fuss if the versions are different. I'm not sure if jstack behaves the same way.
I'm not saying this is the ideal solution, just that it would work. I think jdigital and Eddie's suggestions are better first bets, and even though this shouldn't interfere with an existing java installation the same way running the installer would, the customer may disagree regardless.
jstack and jps are part of tools.jar of the JDK.
Also attach.dll is required to attach jstack to a process.
Ofcourse the tools.jar and attach.dll are not part of JRE.
To make jstack work on a systems which has no JDK (mostly Windows), I usually do the following.
Copy tools.jar and attach.dll from JDK and put in to some location
on the target system. Example: to c:\temp\jstack
Write a bat script to manually invoke it using JRE.
For example, create a bat file jstack.bat:
set JRE=c:\jrefolder
"%JRE%\bin\java" -classpath "c:\temp\jstack\tools.jar" -Djava.library.path="c:\temp\jstack" sun.tools.jstack.JStack %*
Similarly for jps, create a jps.bat with following content.
set JRE=c:\jrefolder
"%JRE%\bin\java" -classpath "c:\temp\jstack\tools.jar" -Djava.library.path="c:\temp\jstack" sun.tools.jps.Jps %*
Usage:
jstack.bat -l <pid>
Hope this helps.
Would you be able to use JConsole via remote access?
To get a thread dump with only a JRE you need tools.jar and attach.dll from the JDK of the same Java version. Install this somewhere and copy these into the jre. Must be identical version!
If you need a dump of a process running under the system account you can use the Windows sysinternals psexec.exe to gain access to the process. Copy this into the JRE bin or somewhere in the path.
This batch file writes the stack dump to a file with a datetime filename so multiple traces can be taken and compared easily.
Threads.bat
:: Creates a thread dump for the tomcat6.exe process
:: saved in a timestamped filename and views it!
:: Jim Birch 20111128 rev 2015-10-12
::Required the following files to be placed in the jre/bin folder:
:: attach.dll - From the Java JDK (must be the same version)
:: tools.jar - ditto
:: psexec.exe - from Windows sysinternals
::cd to jre/bin
d:
cd \application\jre\bin
::build datetime filename
rem datetime from wmi.exe
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set dt0=%%I
rem datetime string as YYYY-MM-DD-hhmmss
set dt=%dt0:~0,4%-%dt0:~4,2%-%dt0:~6,2%-%dt0:~8,6%
set ff=td-%dt%.txt
echo filename: %ff%
::PID of the process by named exe, eg, tomcat6
for /F "tokens=2" %%I in ('TASKLIST /NH /FI "IMAGENAME eq tomcat6.exe"' ) DO SET PID=%%I
echo pid: %PID%
::combine above with jstack command
psexec -s jstack.exe -l %PID% >> %ff%
:: view result
start %ff%
::insert pause to debug or timer to review script operation
::ping localhost -n 20 >nul
::pause
If you want to use the on-board tools of the JDK and also want to both have a minimal (i.e., not including copying the whole JDK) and convenient (i.e. not invoking with a custom .bat) solution, this works for me (tried on Java 1.8):
Create an empty folder ($DEST below) and copy the following files (from the JDK $JDK_HOME) into bin and lib folders as follows:
Source -> Destination
$JDK_HOME/bin/jps.exe -> $DEST/bin/jps.exe
$JDK_HOME/bin/jstack.exe -> $DEST/bin/jstack.exe
$JDK_HOME/bin/jli.dll -> $DEST/bin/jli.dll
$JDK_HOME/jre/bin/attach.dll -> $DEST/bin/attach.dll
$JDK_HOME/lib/tools.jar -> $DEST/lib/tools.jar
Then ZIP and copy this over to the destination machine running a compatible JRE.
You should be able to run jps and jstack from the bin folder now as you would run them from the original JDK.
Related
Getting the following error when attempting to load the Oracle Weblogic installer:
tsugonko-1: ~/weblogic
→ java -jar fmw_12.2.1.4.0_wls_quick.jar
Launcher log file is /private/var/folders/jk/jjhksz9n6c354s25_pvj9qp80000gn/T/OraInstall2022-01-13_11-44-45AM/launcher2022-01-13_11-44-45AM.log.
Extracting the installer . . . . Done
Checking if CPU speed is above 300 MHz. Actual 2662.4 MHz Passed
Checking swap space: must be greater than 512 MB. Actual 145636 MB Passed
Checking if this platform requires a 64-bit JVM. Actual 64 Passed
Checking temp space: must be greater than 300 MB. Actual 145636 MB Passed
Preparing to launch the Oracle Universal Installer from /private/var/folders/jk/jjhksz9n6c354s25_pvj9qp80000gn/T/OraInstall2022-01-13_11-44-45AM
Unrecognized option: -d64
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
1. First, I have verified that the installer script is present, but don't know how to verify that it is actually loading:
tsugonko-1: /private/var/folders/jk/jjhksz9n6c354s25_pvj9qp80000gn/T/OraInstall2022-01-13_11-44-45AM
→ ls
OPatch launcher2022-01-13_11-44-45AM.log oui
install13817544693051062390.sh oracle_common
2. I believe my $JAVE_HOME path is correct:
→ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home
→ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin
(notice that "bin" is not included in the /usr/libexec path, I removed the /bin from the $JAVA_HOME path, but that did not correct the issue)
3. And I believe I have correctly listed the PATH in the ~/.bash_profile (Mac OS, no .bashrc, amended file output to the actual PATH variable
tsugonko-1: /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/bin
→ cat ~/.bash_profile
#!/usr/bin/env bash
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin
export PATH=/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin:$PATH
export ORACLE_HOME=~/Applications/Oracle
(Notice I have two variable paths, I commented one out then the other, but this did not correct the issue either), internet search suggested both, but I assume one is best practice)
Any suggestions from the group? Incidentally, I did have weblogic server installer, but it got corrupted: I could load the admin server, but not any managed servers, so I deleted the files (rm -rf weblogic).
Is it possible I did not properly uninstall weblogic and there is a zombied process preventing the installer to hook the JVM or otherwise not load?
WebLogic Server 12.2.1.4 does not support java 16 but only java 8 and java 11.
Also, it is only supported with Oracle JDK and not OpenJDK.
I am trying to install the gem rjb and I have come across this error
extconf.rb:53:in <main>': JAVA_HOME is not set. (RuntimeError).
The following is the entire error log.
$sudo gem install rjb -v '1.4.9' --source 'https://rubygems.org/'
[sudo] password for santoshpavan:
Building native extensions. This could take a while...
ERROR: Error installing rjb:
ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.5.0/gems/rjb-1.4.9/ext
/usr/bin/ruby2.5 -r ./siteconf20200411-19244-10jhs3k.rb extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/bin/$(RUBY_BASE_NAME)2.5
extconf.rb:53:in `<main>': JAVA_HOME is not set. (RuntimeError)
extconf failed, exit code 1
Gem files will remain installed in /var/lib/gems/2.5.0/gems/rjb-1.4.9 for inspection.
Results logged to /var/lib/gems/2.5.0/extensions/x86_64-linux/2.5.0/rjb-1.4.9/gem_make.out
I tried the answer that is mentioned here.
When I printed the env I can see the JAVA_HOME path mentioned, as below.
santoshpavan#DESKTOP-ISVIQCL:/mnt/c/code/expertiza$ env
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
HOSTTYPE=x86_64
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=C.UTF-8
OLDPWD=/mnt/c
WSL_DISTRO_NAME=Ubuntu-18.04
JAVA_HOME=/mnt/c/Program Files/Java/jdk-10.0.2
S_COLORS=auto
USER=santoshpavan
RBENV_SHELL=bash
PWD=/mnt/c/code/expertiza
HOME=/home/santoshpavan
NAME=DESKTOP-ISVIQCL
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SHELL=/bin/bash
TERM=xterm-256color
SHLVL=1
LOGNAME=santoshpavan
PATH=/home/santoshpavan/.yarn/bin:/home/santoshpavan/.config/yarn/global/node_modules/.bin:/home/santoshpavan/.rbenv/plugins/ruby-build/bin:/home/santoshpavan/.rbenv/shims:/home/santoshpavan/.rbenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu18.04onWindows_2020.1804.7.0_x64__79rhkp1fndgsc:/mnt/c/Python37/Scripts/:/mnt/c/Python37/:/mnt/c/Python27/:/mnt/c/Python27/Scripts:/mnt/c/Users/santo/AppData/Local/Programs/Python/Python37-32/Scripts/:/mnt/c/Users/santo/AppData/Local/Programs/Python/Python37-32/:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/libnvvp:/mnt/c/app/santo/product/12.1.0/dbhome_1/bin:/mnt/c/Program Files/Java/jdk-10.0.2/bin:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/nodejs/:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2019.4.0/:/mnt/c/Apache/apache-zookeeper-3.5.6-bin/bin:/mnt/c/Program Files/Java/jdk-10.0.2/bin:/mnt/c/Spark/spark-3.0.0-preview2-bin-hadoop2.7:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files/PostgreSQL/12/bin:/mnt/c/Program Files/PostgreSQL/12/lib:/mnt/c/Program Files/Intel/WiFi/bin/:/mnt/c/Program Files/Common Files/Intel/WirelessCommon/:/mnt/c/Users/santo/.cargo/bin:/mnt/c/Ruby27-x64/bin:/mnt/c/Program Files/Git/usr/local/bin:/mnt/c/Program Files/Git/bin:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/iCLS/:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/iCLS/:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/WINDOWS/System3:/mnt/c/Program Files/JetBrains/PyCharm Community Edition 2019.3.4/bin:/snap/bin
WSLENV=JAVA_HOME/p
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/env
I do no understand the issue here as JAVA_HOME is present in the environment variables with the WSLENV.
Note:
Earlier, when I had the same error, following this helped. But the javac was still was not being detected. So, I installed java in wsl using $ sudo apt install default-jdk. And, after that this issue has happened.
TL;DR: In WSL, you must use javac.exe since it is a Windows binary. Simply typing javac will not work, even if the path is set up correctly. If that doesn't work, try adding ../bin to the end of your JAVA_HOME variable.
Using Windows Binaries & Environment Variables in WSL
If you have JavaSDK installed on Windows, you do not need to install Linux binaries. Simply ensure that they are properly linked so WSL knows where to find the SDK.
Also, when calling a Windows binary from WSL, you cannot type javac, you must type javac.exe so WSL knows we're looking in the Windows file system.
Ensure Java for Windows works
Open PowerShell or cmd.exe from any directory and type java --version
You should get a list of JRK info:
openjdk 11.0.4 2019-07-16 LTS
OpenJDK Runtime Environment Corretto-11.0.4.11.1 (build 11.0.4+11-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.4.11.1 (build 11.0.4+11-LTS, mixed mode)
Your version might be different, the important part is that the system knows where to find Java. If you get an error, ensure your Windows Environment variables are set correctly:
JAVA_HOME as an Environment Variable, and
JAVA_HOME/bin as a Path variable.
Setting Variable in WSL
The best place to put the next lines of code are in your .bashrc file (I have mine right before sourcing my aliases & functions), but if you are running multiple users or simply prefer ./bash_profile or /etc/profile put it there.
# Shared environment variables
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10
While we're at it, add any other Environment Variables you will need in your development setup.
I have my WSL, Java, and all my other dev tools set up on my second HDD which is not a system drive, ensure that your location matches your JAVA_HOME path in Windows.
If JavaSDK is located at: C:\Java\jdk8.0
The corresponding WSL mount point is: /mnt/c/Java/jdk8.0
Executing
Important: Use java.exe <args> in WSL instead of java <args>
Say you just wrote CompareTwoStrings.class and want to compile and run it using the Windows binaries. You can do it from a Windows shell or WSL.
From Windows Shell:
javac GetStringLength.java
java GetStringLength
From WSL:
javac.exe GetStringLength.java
java.exe GetStringLength
Using java <args> in WSL will result in a Command 'java' not found error. That is because running windows binaries from within WSL requires that the .exe extension is used in the command.
Simplicity
We don't want to install a second copy of Java specific to WSL and waste that precious disk space, so we're going to call the Windows binary from the WSL shell. This is a great benefit of WSL (WSL1 in particular) in that it can interact (almost) flawlessly with the Windows File System.
NOTE: In order to run a program, it must either be a Path variable, or be run from within it's containing folder.
Hopefully that works as easily for you as it did for me. Just remember to use the correct command depending on what OS binary you're running. This took me about 10 minutes to get set up, and has been a lifesaver for cross-compiling and general ease-of-use.
I'm trying to run the JMeter test using the TFS build definition. I've created the required task to run the JMeter test (command-line task), but I'm getting the following error:
Not able to find Java executable or version. Please check your Java installation.
errorlevel=2
Press any key to continue . . .
I've checked the java version on the agent:
I've mentioned the following system variables:
JMETER_HOME as C:\Performance Tests\apache-jmeter-3.2
JRE_HOME as "C:\Program Files\Java\jre1.8.0_231"
to the Path variable was added C:\Performance Tests\apache-jmeter-3.2\bin;C:\Program Files\Java\jre1.8.0_231\bin
When I try to run the JMeter on the agents I don't get any error:
Windows Server 2012 R2 64 bit is installed on the agent.
On another agent (which is the same and has similar configs) the test is running with no issues.
Could someone tell me what is the reason for the issue? Thank you in advance 😊
Looking into jmeter.bat source:
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
rem #echo Debug Output: %%g
set JAVAVER=%%g
)
if not defined JAVAVER (
#echo Not able to find Java executable or version. Please check your Java installation.
set ERRORLEVEL=2
goto pause
)
it appears that the JMeter startup script is not capable of parsing the output of java -version command in order to determine Java runtime version
So make sure to have "bin" folder of your JRE or JDK installation added to your operating system PATH environment variable
In order to have 100% confidence that it will work add this line to the beginning of your jmeter.bat script:
set PATH="C:\Program Files\Java\jre1.8.0_231\bin";%PATH%
also be aware that according to 9 Easy Solutions for a JMeter Load Test “Out of Memory” Failure article you should always be using the latest version of JMeter so consider upgrading to JMeter 5.2.1 (or whatever is the latest stable version available at JMeter Downloads page) on next available opportunity
I'm doing a year in computer science at my university and it turns out there are very few computers that have the jdk installed on them. I can run eclipse portable from my memory stick, so compiling java should be possible.
What I was wanting was a batch file that set up the environment variables on the system so that I am able to run the portable version of the jdk from my memory stick. I don't appear to be able to alter the system variables but can change the user ones.
So far I have the below code as a batch file, though it still leaves me with a javac not recognize message when run. As a final note I should point out I don't have a great deal of experience with batch files.
echo %path% > stored_path.txt
setx PATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%PATH%"
setx JAVA_HOME "%cd%PortableApps\CommonFiles\OpenJDK"
setx CLASSPATH "%cd%PortableApps\CommonFiles\OpenJDK\bin;%CLASSPATH%"
Thanks for any help you can give me.
Update 04/10/13 - 16:54:
Just to clarify Eclipse itself works fine and is fully usable. However I have a jdk saved on the memory stick that works as if I call javac from a console that is open to that directory it works fine. What I want to know is if there is a way for windows to recognise this command from any directory on the system by just changing the user environment variables.
A thought that just occurred to me (I can't test it right now), is that would making a shortcut to the javac exe file in another folder work?
No need to actually set the environment variables permanently!
Just set them for the current session of the program. This is the batch script I made to launch my eclipse setup.
#echo off
setlocal
pushd "%~dp0"
if not exist eclipse.exe exit /b 1
if not exist jdk1.6.0_45\ exit /b 2
if not exist apache-maven-2.2.1\ exit /b 3
set "JAVA_HOME=%~dp0\jdk1.6.0_45"
set "MAVEN=%~dp0\apache-maven-2.2.1"
set "M2_HOME=%MAVEN%"
set "MAVEN_OPTS=-Xms512m -Xmx1024m"
set "CLASSPATH=.;%JAVA_HOME%\jre\lib\ext;%CLASSPATH%"
set "PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%MAVEN%\bin;%PATH%"
rem M2_REPO
mklink /d jre "jdk1.6.0_45\jre"
start eclipse.exe -vm "%JAVA_HOME%\bin\javaw.exe" -data ".\workspace"
rem cmd /k
popd
endlocal
exit /b 0
This sets all that I need and starts eclipse with these settings without having to modify the variables.
My folder structure:
eclipse\
jdk1.6.0_45\
apache-maven-2.2.1\
workspace\
eclipse.exe
all other eclipse folders and files.
Our school-computers didn't have git installed, so i put the portable version of msysgit on my USB-Stick. That gives you (aside from git) a portable MSYS, which is a bourne shell with most of the standard linux tools (like ls and df).
So, i used the shell, instead of the windows Command Line. Then, for the shell, I created a small bash-script to set up the environment-path's. From the gist:
To make the script ("env_setup.sh") execute every time the git-shell
is started, you'll want to put the script into the "etc/"-directory in
your msysgit-folder.
After that, you'll need to edit the "profile"-file, which is executed
by the shell every time it's launched.
Add the following line at
the end of the file:
# Setup the environment-variables
. /etc/env_setup.sh
This is the script, which sets up the environment variables:
#!/bin/bash
# Resources:
# http://stackoverflow.com/questions/623518
# http://stackoverflow.com/questions/59895
# http://markashleybell.com/articles/portable-git-windows-setting-home-environment-variable
# Get the directory this script is in:
DIR=$(cd $(dirname "$0"); pwd)
# Get only the Flash-Drive letter (e.g. "F")
FLASH=${DIR:1:1}
echo "We determined your USB-drive to be on $FLASH:\\"
# Set the home-path to the Flash-drive to get portable SSH-keys:
# --- You'll want to change this to your prefered Home-directory!
export HOME=/$FLASH/PortableApps/git/home/luke
echo "I set your Home-directory to '$HOME'";
### --- EXAMPLES ---
# Set the Java-Home variable to a JDK on USB-Stick:
export JAVA_HOME=/$FLASH/JDK1.6
# Add the Java-Tools to the JDK-folder:
export PATH=$PATH:/$FLASH/JDK1.6/bin
# Add a Maven from your USB-drive to the PATH:
export PATH=$PATH:/$FLASH/PortableApps/apache-maven/bin
# Add Node.js from the local pc to your PATH:
export PATH=$PATH:/c/Programms/nodejs
Here, you can see a few examples, including a JDK I had on the same USB-Stick. It is added to the PATH, so you can write javac everywhere you like and always use the JDK specified.
Note that this is only for one session and that it only works in the MSYS shell!
I'm writing an application that leverages jsvc to start up a Java service as a daemon. I need to use something like jsvc because my application utilizes ports under 1024 and yet I'd really like to not run it as root so that created files are owned by another user. I'd also like to keep dependencies and configuration to a minimum so that all the client needs is a JVM and the jsvc binary installed.
However, it seems that jsvc has one major catch; it can't detect the home folder of Java on a given Unix operating system, which is quite frustrating:
$ ./startup.sh
Cannot locate Java home
I have been able to work around the issue on Ubuntu at least by manually setting the JVM home directory:
jsvc ... -home /usr/lib/jvm/default-java/ ...
Is there any way to determine the Java home directory dynamically from a Bash script so I can make this work across most Unixes/Linuxes? I'd be able to sleep much better at night doing something like:
JAVA_HOME="$( ... )"
jsvc ... -home "$JAVA_HOME" ...
...rather than hard-coding for each individual operating system. Is there a way that, given a java binary, I can find the home directory of its JVM/JRE?
Not sure if this works across *nixes, but found this solution:
JAVA_HOME="$( readlink -f "$( which java )" | sed "s:bin/.*$::" )"
I've tested it on Ubuntu and it works, however it does not work for OSX.
My solution was compiling the native linux source as the main jsvc page says in
http://commons.apache.org/proper/commons-daemon//jsvc.html
Here is my step by step procedure
Download www.fightrice.com/mirrors/apache/commons/daemon/source/commons-daemon-1.0.13-src.tar.gz
Once you extract the file then go to ...../commons-daemon-1.0.13-src/src/native/unix
in terminal as a root do the following:
$ support/buildconf.sh
$ ./configure --with-java=/usr/lib/jvm/default-java
$ make
test generated jsvc binary app
$ ./jsvc -help
It works! cleanly.
Use dirname and which commands to find Java's bin directory:
echo `dirname \`which java\``
JAVA_HOME=`dirname \`which java\``
... Only works if Java is already on the $PATH.
One other way is :
type -p java
Expect this to return the correct JAVA installation folder.