LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-22-2017, 07:17 PM   #1
WFV
Member
 
Registered: Apr 2012
Location: CA
Distribution: Arch
Posts: 181

Rep: Reputation: Disabled
set java environment specific for launching an application?


Is there a way to set the java environment specifically for launching an application?
or set the application to select the correct java version if different from the default assuming the required version is available?
X = pdfsam requires java 8. Java 7, 8, and 9 are installed and 9 is default.
Y = I'd like to keep 9 as default but when I want to use pdfsam I want it to ignore default and use required 8 version.

EDIT1: /usr/bin/pdfsam.sh shows >=8 but doesn't work with 9:
Code:
#!/bin/sh
# run application with java env >= 8

PDFSAM_EXECUTABLE="/usr/share/java/pdfsam/bin/pdfsam.sh"
PDFSAM_JAVA_VERSION="8\|9"

run_pdfsam() {
    exec "${PDFSAM_EXECUTABLE}"
    exit 0
}

# check JAVA_HOME first
if test -n "${JAVA_HOME}"; then
    if echo ${JAVA_HOME} | grep -q "${PDFSAM_JAVA_VERSION}"; then
        # system environment variable set to required, do nothing
        echo "Using java environment from JAVA_HOME: ${JAVA_HOME}" >&2
        run_pdfsam
    else
        # unset JAVA_HOME
        echo "Unset JAVA_HOME for this run: ${JAVA_HOME}" >&2
        unset JAVA_HOME
    fi
fi

# check current environment
if archlinux-java status | grep "default" | grep -q "${PDFSAM_JAVA_VERSION}"; then
    # required java version is set to default, do nothing
    echo "Using system enabled java environment" >&2
    run_pdfsam
fi

# find valid environment
PDFSAM_JAVA_ENV="`archlinux-java status | grep "${PDFSAM_JAVA_VERSION}" | awk '{ print $1 }'`"
export PATH="/usr/lib/jvm/${PDFSAM_JAVA_ENV%/*}/jre/bin/:${PATH}"
echo "Run with exported ${PDFSAM_JAVA_ENV}" >&2
run_pdfsam
Thanks anyone for considering.

EDIT2: looks like this is the answer

Last edited by WFV; 12-23-2017 at 12:32 AM.
 
Old 12-23-2017, 04:30 AM   #2
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Outside, most of the time.
Posts: 868
Blog Entries: 26

Rep: Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527Reputation: 527
Yes. Java is all about Path's. Profound knowledge of these is necessary to achieve anything with that language and defaults are more often obstructing serious work than facilitating anything. Worse even, if you develop.
 
Old 12-23-2017, 05:56 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,622

Rep: Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369
Yes, write a wrapper script or a function (like run_pdfasm) which will set java properly if required, do not care about the current settings.
And some additional comments:
Code:
run_pdfsam() {
    exec "${PDFSAM_EXECUTABLE}"      # do not exec shell scripts, but binaries
    exit 0                           # this will not be executed, the exec never returns (only in case of an error)
}
Code:
PDFSAM_JAVA_ENV="`archlinux-java status | grep "${PDFSAM_JAVA_VERSION}" | awk '{ print $1 }'`"
do not use grep and awk together, you can implement it only within awk:
Code:
grep pattern | awk '{script}'
# often can be written as:
awk '/pattern/ {script}'
 
1 members found this post helpful.
Old 12-23-2017, 01:14 PM   #4
WFV
Member
 
Registered: Apr 2012
Location: CA
Distribution: Arch
Posts: 181

Original Poster
Rep: Reputation: Disabled
Thank you Michael and pan64.
@ pan64
Quote:
do not use grep and awk together, you can implement it only within awk:
the line in /usr/bin/pdfsam.sh would then be?
Code:
PDFSAM_JAVA_ENV="`archlinux-java status | awk '${PDFSAM_JAVA_VERSION} { print $1 }'
(i'm completely awk illiterate - but can test it, it will either work or not, then on to read up on awk...)

And for Y, I understand the Wiki to just write a new launcher to run pdfsam with the desired java version.
The packagers launch script looks like its intended to use at minimum java version 8 regardless of default, but it is not working when my default is set to 9.
 
Old 12-23-2017, 01:28 PM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,622

Rep: Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369Reputation: 4369
no, you missed two things: variable is not evaluated between ' ', therefore you need to use " ". The other is the slash, so:
Code:
awk "/${PDFSAM_JAVA_VERSION}/"'{ print $1 }'
Oh yes, and no need for backslash, so
Code:
PDFSAM_JAVA_VERSION="8|9"
 
1 members found this post helpful.
Old 12-23-2017, 03:29 PM   #6
WFV
Member
 
Registered: Apr 2012
Location: CA
Distribution: Arch
Posts: 181

Original Poster
Rep: Reputation: Disabled
Thanks again pan64 - those changes work fine so long as default version is java 8. I haven't tried the Wiki workaround yet but just for anyone who may be interested, these are the errors that happen when default java environment is set to 9:
Code:
% pdfsam
Run with exported java-8-openjdk
java-9-openjdk
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/scene/layout/HBox
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
	at org.pdfsam.community.App.main(App.java:34)
Caused by: java.lang.ClassNotFoundException: javafx.scene.layout.HBox
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
	... 10 more
so looks like pdfsam attempts to use 8 (exported) but fails to launch. Also notice that I can't set java default to "java-9-openjdk/jre", only "java-9-openjdk" and both jdk and jre are installed, maybe unrelated to this though.

EDIT: marking this solved, thanks for help. I followed the Wiki and wrote a little wrapper to /usr/bin, then redirected the desktop exec= to it, reset java default to 9, and pdfsam now launches without issue via Xfce Applications menu as well as from terminal.

Last edited by WFV; 12-24-2017 at 01:35 AM.
 
Old 12-24-2017, 11:06 AM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,743

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by Michael Uplawski View Post
Yes. Java is all about Path's. Profound knowledge of these is necessary to achieve anything with that language and defaults are more often obstructing serious work than facilitating anything. Worse even, if you develop.
Yeah... "Write once, run anywhere." Ri-i-i-ight.

Any system that is used for more than a single application seems to run into this mess and knowing which environment variables to define/export before launching the application is critical.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running Java Application with no desktop environment. cslucsm Linux - Newbie 4 09-14-2010 09:45 AM
Running Java Application with no desktop environment. cslucsm Linux - General 3 09-10-2010 01:45 PM
set specific application to always reside in ram, for +performance Siljrath Linux - General 2 06-13-2010 01:31 AM
giving sudo privliges to run *specific* java application dsollen Linux - Security 2 06-25-2009 10:39 AM
set-up of application in freeBSD and linux environment. ratul_11 Linux - Newbie 3 12-14-2005 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration