Opened 20 months ago

Last modified 20 months ago

#58098 new enhancement

Install openjdk8 as default JVM

Reported by: giansalvo (gians) Owned by:
Priority: Normal Milestone:
Component: ports Version: 2.5.4
Keywords: Cc: breun (Nils Breunese)
Port: openjdk8

Description

I'm trying to port a java based application (Arduino IDE) to MacPorts. The Arduino application is built correctly and Arduino.app get installed to /Applications/MacPorts folder.
However when I launch Arduino.app I get "Unable to load Java Runtime Environment".

I installed openjdk8 with sudo 'port install openjdk8'.
I have another JVM installed that looks like the default JVM (see attached files):

$ls  /Library/Java/JavaVirtualMachines/
jdk1.8.0_131.jdk	openjdk8

What is (if it's possible) the clean way to install openjdk8 as the default JVM?

Attachments (4)

Schermata 2019-02-18 alle 21.35.56.png (56.0 KB) - added by giansalvo (gians) 20 months ago.
Schermata 2019-02-18 alle 21.40.33.png (254.7 KB) - added by giansalvo (gians) 20 months ago.
Schermata 2019-02-19 alle 18.01.56.png (398.7 KB) - added by giansalvo (gians) 20 months ago.
Info-openjdk.plist (2.5 KB) - added by giansalvo (gians) 20 months ago.
this is Info.plist file found inside the macOS app bundle

Download all attachments as: .zip

Change History (15)

Changed 20 months ago by giansalvo (gians)

Changed 20 months ago by giansalvo (gians)

comment:1 Changed 20 months ago by ryandesign (Ryan Schmidt)

I don't know.

My understanding is ports should use the java 1.0 portgroup to indicate what JDK they want.

comment:2 Changed 20 months ago by jmroot (Joshua Root)

Cc: breun added

Are you just wanting to get this one port to use a specified JRE? That's different to setting it as the default.

comment:3 Changed 20 months ago by breun (Nils Breunese)

The standard way of setting your default JVM is by setting the JAVA_HOME environment variable.

When you install the openjdk8 port this message is displayed explaining this:

If you have more than one JDK installed you can make JDK 8 the default
by adding the following line to your Bash shell profile (~/.bash_profile):
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8/Contents/Home

You can check the value of JAVA_HOME by running echo $JAVA_HOME. On my machine I get this:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/openjdk8/Contents/Home

What do you get on your machine?

You can run /usr/libexec/java_home -V to get a list of all JVM's on your machine (as long as they're installed under /Library/Java/JavaVirtualMachines).

Another method is using /usr/libexec/java_home to find a JVM matching the desired Java major version like this:

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/openjdk8/Contents/Home

You could use that command in your Bash profile like this:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Although I prefer to set JAVA_HOME to an explicit location myself.

Changed 20 months ago by giansalvo (gians)

comment:4 in reply to:  3 Changed 20 months ago by giansalvo (gians)

Replying to breun:

The standard way of setting your default JVM is by setting the JAVA_HOME environment variable.

When you install the openjdk8 port this message is displayed explaining this:

If you have more than one JDK installed you can make JDK 8 the default
by adding the following line to your Bash shell profile (~/.bash_profile):
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8/Contents/Home

Yes I know. This is true for java applications that you launch from the terminal/shell. But I need to "link" the JVM with a macOS bundled java application (Arduino.app in the /Applications folder). As far as I could see the JAVA_HOME shell variable set in the Bash profile configuration file doesn't affect the .app application.

Furthermore as you can see from the third attached image, the .app file contains already the entire openjdk8 JRE.

comment:5 in reply to:  2 Changed 20 months ago by giansalvo (gians)

Replying to jmroot:

Are you just wanting to get this one port to use a specified JRE? That's different to setting it as the default.

I actually would like to link this specific port to openjdk8 JRE (which is already contained in the .app file). At worst I'd like to try setting it for the all system.

comment:6 Changed 20 months ago by breun (Nils Breunese)

If the app already contains an entire JRE, then I think it won't be necessary to have the openjdk8 port installed. I'm not very familiar with macOS GUI apps that use Java, but I guess the .app must contain some kind of start script that starts the java process with the correct arguments (using the java binary from the included JRE). Maybe you can take a look at the package contents of another macOS GUI app on how to do this. Dealing with the JAVA_HOME environment variable indeed shouldn't be necessary in this case.

Last edited 20 months ago by breun (Nils Breunese) (previous) (diff)

comment:7 Changed 20 months ago by kencu (Ken)

Alternatively there is a way to set environment variables using the plist file in the bundle that might be of use for this. You could set it to a specific JAVA_HOME perhaps.

<https://superuser.com/questions/476752/setting-environment-variables-in-os-x-for-gui-applications>

comment:8 in reply to:  6 Changed 20 months ago by giansalvo (gians)

Replying to breun:

If the app already contains an entire JRE, then I think it won't be necessary to have the openjdk8 port installed.

The app was built WITH openjdk8 and should contain it but somehow it isn't recognized or something like that.

Replying to breun:

I'm not very familiar with macOS GUI apps that use Java, but I guess the .app must contain some kind of start script that starts the java process with the correct arguments (using the java binary from the included JRE). Maybe you can take a look at the package contents of another macOS GUI app on how to do this.

It's exactly what I thought but I couldn't find any macOS GUI Java app already ported in MacPorts. Do you know any?

Changed 20 months ago by giansalvo (gians)

Attachment: Info-openjdk.plist added

this is Info.plist file found inside the macOS app bundle

comment:9 Changed 20 months ago by breun (Nils Breunese)

I'm not familiar with macOS GUI Java apps, but that Info.plist contains references to $JVM_RUNTIME. I don't know if that has a value and if so, where it would come from. The screen shot you attached also shows the openjdk8 dir to be in Contents/PlugIns/openjdk8, while the plist references $JVM_RUNTIME/Contents/Java (I don't know what's in there). If the JRE really is contained in the .app, then I don't think you'd need an environment variable like $JVM_RUNTIME, but should just be able to point to the Java directory inside the .app directly. I don't really think this is really a MacPorts question actually, maybe a place with people who know how to create a macOS .app with a JRE inside will be able to help you better.

comment:10 Changed 20 months ago by giansalvo (gians)

I was able to fix the problem using /usr/libexec/java_home to set JAVA_HOME environment variable.

comment:11 Changed 20 months ago by breun (Nils Breunese)

That method will not use the included JRE though, because /usr/libexec/java_home won't find that, so then users will need to have a separate Java installation.I guess it should be possible to point JAVA_HOME to the included JRE instead.

Note: See TracTickets for help on using tickets.