How to use different java versions for different programs? - shell

This may be common in web but may be I am not getting what to search exactly. Actually I am having different versions of java on my ubuntu server having java 1.5 as default. One of my program need java 1.6 and even if I perform following in the shell script executing that program
export JAVA_HOME=/path/to/jdk6
I dont get any success.What I learnt is I could see current java version using
java -version
This only get change when I changes the alternative using:
sudo update-alternatives --config java
Please tell me how can I change it in the shell script which in turn calls the java program requiring java6 having java 5 as default only.
Thanks!!!

You need to update the PATH as well:
JAVA_HOME=/path/to/jdk6
export JAVA_HOME
PATH=${JAVA_HOME}/bin:${PATH}
export PATH

Related

Code in bash script that changes version of java

I have to preparae a script that
-switches the current Java to this one: /usr/bin/java
export PATH=$JAVA_HOME”$PATH” export JAVA_HOME=”/usr/bin/java”
What should I add to it to work?
/usr/bin/java is not a directory, but an executable file.
The JAVA_HOME should point to Java's installation directory, if at all required. Most of the time, you don't actually need to define JAVA_HOME as it's only used by certain applications (such as Maven) to override the version of Java that would otherwise be found on the PATH.
So all you need to do is:
export PATH=/usr/bin:$PATH
and this will make the java command be taken from the /usr/bin directory. This is normally in your path anyway, but this line ensures it's at the start.
You may also need to delete any existing definition of the JAVA_HOME variable, as that would override /usr/bin/java when used by tools such as Maven.

Basic Maven installation on Mac OS - blank main class error

This is a very basic but bizarre malfunctioning that is specific to my environment but not being a systems programmer I'm not sure how to debug this.
Command
wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.zip
unzip apache-maven*zip
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home \
M2_HOME=/Volumes/Apps/apache-maven-3.8.5/ \
/Volumes/Apps/apache-maven-3.8.5/bin/mvn --help
Output
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Error: Could not find or load main class '
Caused by: java.lang.ClassNotFoundException: '
(there is no actual class name)
Incorrect answer
Anything involving pom.xml (I am just verifying the installation)
Notes
Inside an Ubuntu docker container the same thing works fine (with JDK 17)
I tried deleting the ~/.m2 dir
I've tried with JDK 8
brew install mvn gives the same problem
Gradle and Tomcat (my more common tools) work fine
Usually I use zsh but the same issue happens in /bin/sh and /bin/bash
same issue with mvnDebug and mvnyjp
I used maven for 10+ years without seeing this before, so I'm no rookie
same issue with apache-maven-3.2.5-bin.zip
First only use the environment variable JAVA_HOME do not use M2_HOME (not needed nor helpful). Furthermore use separate lines for exporting the environment variables (see https://maven.apache.org/install.html) ...you have concatenated the lines JAVA_HOME...with \ which means it's only a single line. Do not do that.
Download the zip file if you like (better would be the tar.gz variant on MacOS).
the following three commands in a terminal:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
export PATH=/Volumes/Apps/apache-maven-3.8.5/bin:$PATH
mvn --version
The message you got while starting the JVM: Ignoring option MaxPermSize; support was removed in 8.0 means you have somewhere in your environment a variable like JAVA_OPTS set with some options which are not correct.

Grails 2.4.0 on Mac OS X extension class fails to load

After downloading and installing Grails 2.4.0, I get the following warning when running any grails command, including grails -version:
WARNING: Module [groovy-all] - Unable to load extension class [org.codehaus.groovy.runtime.NioGroovyMethods]
A web search revealed this post about groovy: http://permalink.gmane.org/gmane.comp.lang.groovy.user/64538
And this one, which includes, but is not specifically about this warning:
https://jira.grails.org/browse/GPFILTERPANE-125
I am running Mac OS X 10.9.3, java version "1.7.0_40".
Is this a spurious error that can be ignored? So far, grails seems to be working fine, but I thought I would post about it to see if others have experienced something similar.
The problem is grails sets JAVA_HOME to /Library/Java/Home if JAVA_HOME is not set. Most likely, your system have older jdk (1.6) under that directory. As paranoid mentioned, it requires jdk 1.7. Therefore, even if you have jdk1.7 installed, but grails doesn't use it at all.
Here is the solution to this problem:
Download and install Oracle JDK, which will be installed to /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk (say if you install jdk1.7u51)
Run the following commands to change the system settings if you don't want set JAVA_HOME explicitly
cd /System/Library/Frameworks/JavaVM.framework/Versions
rm CurrentJDK
ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents CurrentJDK
Then the warning should go away.
Install Java 7
Found where is Java 7 Home.
On Mac OS run /usr/libexec/java_home. For example my home is /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
Create env variables GRAILS_HOME, JAVA_HOME and add them to PATH and to .bash_profile
For example contents of my .bash_profile file is:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
export GRAILS_HOME=/Users/admin/work/grails
export PATH=$JAVA_HOME:$GRAILS_HOME/bin:$PATH
Now grails is using jdk7 and is without warnings.
The module groovy.nio.2.3.0.jar requires Java 7 or later, which is why you're getting the error.
You could either upgrade your java version or remove that module. The warning is essentially harmless.

Cygwin failing to find Java

I'm currently trying to configure a standalone Hadoop node on a new computer (for my own learning purposes) but when I try to find the root of my java installation using $which java
as per this question
root of java installation
I get the following error
Me#myhouse /cygdrive/c/Program Files (x86)
$ which java
which: no java in (/usr/local/bin:/usr/bin:/cygdrive/c/Windows/system32:/cygdrive
/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32
/WindowsPowerShell/v1.0:/cygdrive/c/Program Files (x86)/ATI Technologies/ATI.ACE/Core-Static)
I get the exact same error message at the top level of my C: drive.
I know that Java is there - I just installed it! Can anyone explain what I have done wrong?
Your command just checks if java is found in the common paths from cygwin
So in cygwin you need to add the java path to the paths: export JAVA_HOME="/cygdrive/c/Program\ Files/Java/jre1.8.0_102" export PATH="$PATH:$JAVA_HOME/bin"

How to install yui compressor in CentOs

I know this is going to sound like a noob question, but I'm new to using linux, I was trying to find out how to install yui-compressor in CentOS, but I haven't been able to find it using google.
I have already done this:
wget http://yui.zenfs.com/releases/yuicompressor/yuicompressor-2.4.6.zip
unzip yuicompressor-2.4.6.zip
# mv yuicompressor-2.4.6/build/yuicompressor-2.4.6.jar /usr/share/yui-compressor/yui-compressor.jar
I've also created a file in /usr/bin called yui-compressor that has:
#!/bin/sh
YUI_JAR=/usr/share/yui-compressor/yui-compressor.jar
I dont know what else to do. (I don't even know if its already installed).
Can someone please help me with the following:
If its installed, please tell me.
If not. Tell me what else do I need to do.
Thank you.
At the most basic level, you will want to update your /usr/bin/yui-compressor script to look something like this:
#!/bin/sh
YUI_JAR=/usr/share/yui-compressor/yui-compressor.jar
java -jar $YUI_JAR "$*"
Ensure that you have a Java runtime installed.
As Jordan mentioned, make sure you have Java installed. Try java -version to see if the JDK info shows up. If not try, which java -- on CentOS it should be in /usr/bin/java if are using the RPM installed version.
If it doesn't show up, use: yum install java-1.6.0-openjdk which should install the JDK and any other required java libs (do this with sudo or as root user). Just make sure you don't already have another JDK installed or you could run into problems later (try rpm -qa | grep java to see what's installed via RPM).
Then you should be able to call the yuicompressor directly with a statement like:
/usr/bin/java -jar /usr/share/yui-compressor/yui-compressor.jar --help
If you get the help output, you probably are ok. You can then use it on a file with a statement like:
/usr/bin/java -jar /usr/share/yui-compressor/yui-compressor.jar /path/to/source/file >> /path/to/exportfile.js
(obviously change the filename paths.) There are a number of options you can use, but that should get you started. I'm using this on CentOS with a build script that loops through various files and creates optimized bundled files and it works well.
You may also want to check out the build script and instructions at: http://html5boilerplate.com/ which uses yui-compressor and is the method I would eventually like to go with.

Resources