Skip to content

fix issue with multiple java runtimes on macOS #52474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2020

Conversation

codeprefect
Copy link
Contributor

@codeprefect codeprefect commented Mar 12, 2020

Description

Flutter developers on macOS with multiple JVM installations encounter an issue where flutter reports an installation error with the installed Android SDK.

I found out that this was due to how Flutter looks for the JDK on macOS.

Below is a screenshot of what the error looks like, and the results after this fix.

image

image

Related Issues

Replace this paragraph with a list of issues related to this PR from our issue database. Indicate, which of these issues are resolved or fixed by this PR. There should be at least one issue listed here.

Tests

I added the following tests:

Replace this with a list of the tests that you added as part of this PR. A change in behaviour with no test covering it
will likely get reverted accidentally sooner or later. PRs must include tests for all changed/updated/fixed behaviors. See Test Coverage.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.
  • Yes, this is a breaking change. If not, delete the remainder of this section.
    • I wrote a design doc: https://flutter.dev/go/template Replace this with a link to your design doc's short link
    • I got input from the developer relations team, specifically from: Replace with the names of who gave advice
    • I wrote a migration guide: Replace with a link to your migration guide

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Mar 12, 2020
@fluttergithubbot
Copy link
Contributor

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@codeprefect codeprefect changed the title fix issue with multiple java runtime on macOS fix issue with multiple java runtimes on macOS Mar 12, 2020
@zanderso
Copy link
Member

@mimam419 If you'd like to continue with this PR, please follow the instructions of the fluttergithubbot and the googlebot. Thanks!

@zanderso zanderso added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 19, 2020
@codeprefect
Copy link
Contributor Author

Hi @zanderso,

I am having difficulties fixing the tests because I am quite new to flutter.

Would you mind guiding me on how to get that sorted?

Thank you.

@@ -583,14 +583,13 @@ class AndroidSdk {
if (platform.isMacOS) {
try {
final String javaHomeOutput = processUtils.runSync(
<String>['/usr/libexec/java_home'],
<String>['/usr/libexec/java_home', '-v', '1.8'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a glance, this does not seem very future proof. Should this not specify a version, and take the most recent available instead? /cc @blasten for thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thought too, but then I realized the error was due to the fact that the tooling was picking the latest version, and it fails whenever that latest version is not v1.8, hence my reason for this approach.

Copy link

@blasten blasten Mar 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you remember which version java_home was defaulting to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, it defaults to 1.13 on my machine, which obviously do not work in flutter.
The first solution was uninstalling other versions of Java, then it would pick 1.8
Basically, it pick the latest version of java based on the output from java_home

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the background information. One more question, did you ever get an error message other than the Flutter doctor failure? I wonder if there are related issues that can be associated with this fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not get any other errors.
In fact, the original error was that "android-sdk" was not installed, after reading through similar errors from here and StackOverflow, I learned it was due to not having the right version of JDK (v1.8) installed.

@zanderso zanderso removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 20, 2020
@zanderso zanderso requested a review from blasten March 20, 2020 16:31
@jmagman
Copy link
Member

jmagman commented Mar 26, 2020

See also #9429

if ((javaHomeOutputSplit != null) && (javaHomeOutputSplit.isNotEmpty)) {
final String javaHome = javaHomeOutputSplit[0].trim();
if ((javaHomeOutput != null) && (javaHomeOutput.isNotEmpty)) {
final String javaHome = javaHomeOutput.trim();
Copy link

@blasten blasten Mar 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it still needs to split by lines and javaHome is the last line instead of the first one. This is to account for a case where java 1.8 isn't installed. The first line becomes an error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is how the scenario went here.
I have two versions of java v1.8 and v1.13 installed.
Having set v1.8 as my default version via jenv, running java -version returns
Screenshot 2020-03-27 at 07 46 39

Running the original code /usr/libexec/java_home returns
Screenshot 2020-03-27 at 07 07 33

The next viable option would have been running /usr/libexec/java_home -V, which returns
Screenshot 2020-03-27 at 07 08 03

Picking the right java_home address there is tricky, and as you can see, even the last line has v1.13.

Since flutter would not accept any other version of java except 1.8 (at least for now).
I feel it is okay to try /usr/libexec/java_home -v 1.8, which returns
Screenshot 2020-03-27 at 07 11 48
Just what we need and nothing more.

Copy link

@blasten blasten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG after the comment is addressed + the test added

@codeprefect
Copy link
Contributor Author

LG after the comment is addressed + the test added

I was having difficulty running the test locally, but I have managed to proceed now.

@codeprefect codeprefect force-pushed the fix_java_version_issue branch from 6be9ece to a38d5d5 Compare March 27, 2020 16:15
@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@codeprefect
Copy link
Contributor Author

LG after the comment is addressed + the test added

Hello @blasten, the tests are also fixed now.

@codeprefect codeprefect force-pushed the fix_java_version_issue branch from a38d5d5 to 9caaec7 Compare March 30, 2020 09:16
if ((javaHomeOutputSplit != null) && (javaHomeOutputSplit.isNotEmpty)) {
final String javaHome = javaHomeOutputSplit[0].trim();
if ((javaHomeOutput != null) && (javaHomeOutput.isNotEmpty)) {
final String javaHome = javaHomeOutput.trim();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final String javaHome = javaHomeOutput.trim();
final String javaHome = javaHomeOutput.split('\n').last.trim();

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running java_home -v 999999, outputs:

Unable to find any JVMs matching version "999999".
/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home

Therefore, it needs to extract the last line, which is the default JVM.

Copy link
Contributor Author

@codeprefect codeprefect Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @blasten, you are suggesting this as a fix for when Java 8 is not installed?
I just understood your point, the requested changes have been effected.
Thank you.

- specify java v1.8 in call to java_home
- added tests to handle the modificstion
@codeprefect codeprefect force-pushed the fix_java_version_issue branch from 9caaec7 to 003e327 Compare March 31, 2020 10:24
Copy link

@blasten blasten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@blasten
Copy link

blasten commented Mar 31, 2020

Thanks for the contribution!

@codeprefect codeprefect deleted the fix_java_version_issue branch March 31, 2020 20:41
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants