JDK 9 + Mac OS + jlink? - java

I installed the release version of JDK 9 on Mac OS.
jshell works great, Jigsaw module support works, but there is no jlink:
➜ java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
This comes up empty:
find /System/Library/Frameworks/JavaVM.framework/Versions/Current/ -iname jlink\*
FYI:
➜ ls -l $(which java)
lrwxr-xr-x 1 root wheel 74 Nov 7 2016 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

You can verify your JAVA_HOME using which java and make sure it points to the default installation path which ideally should be
/Library/Java/JavaVirtualMachines...
[for e.g. I use it as export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/]
and further you can find the jlink in the bin folder of Contents
find /Library/Java/JavaVirtualMachines/jdk-9.jdk -iname jlink\*
which should return
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/bin
Attaching a screenshot for reference of the location its installed:-
Note: Though in the screenshot, the command doesn't run successfully but its recognized.

To add the JDK 9 tools to your path, add the following to the file .bashrc of your home directory:
export JAVA_HOME=$(/usr/libexec/java_home -v 9)
export PATH="$JAVA_HOME/bin:$PATH"
Did you notice the -v 9? you can change that to 1.8 if you ever want to switch back to JDK 1.8. For any newbie who can’t locate .bashrc in the Finder: press ⌘⇧. (command shift dot) to reveal hidden files.

There's some important context here: Understanding Oracle's Java on Mac
The standard JDK utilities (java, javac, etc) are actually installed at /Library/Java/JavaVirtualMachines/(JDK_VERSION)/Contents/Home/bin.
/usr/bin is in the path, and in that directory you'll find all the usual JDK utilities. When you type java (or any other command), those are the ones it finds, but they are actually symlinks to executables with the same names in /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands. Those executables are just wrappers that interrogate your JAVA_HOME environment variable (which should point to the actual install location) and invoke the real binaries found there. I'm unclear on why anyone thought these two layers of abstraction were necessary, but it is what it is.
At some point after the release of Java 9, symlinks and wrappers for jshell were added to macOS, but it appears that no such thing was done for jlink.
To keep things working consistently going forward, I'd recommend writing an equivalent wrapper, putting it in the right location, and added a symlink to it in /usr/bin.
Unfortunately, since the wrapper scripts are under /System, you cannot create or modify anything, even as root, until you disable System Integrity Protection. This takes a few minutes and involves a few reboots, but it's easy to do:
Reboot your machine. While it reboots, hold down ⌘R. This will cause the machine to start up in Recovery Mode. You can release the keys when you see the progress bar.
Once in Recovery Mode, choose Terminal from the Utilities menu.
At the prompt, type csr disable. You'll be prompted to reboot to cause the changes to take place. Do that as well.
Once you're back in regular mode, open Terminal and do the following:
% sudo vi /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
Password:
Enter your password at the prompt.
In vi, switch to Insert mode by pressing I, then enter the following text:
#!/bin/bash
$JAVA_HOME/bin/jlink $#
Then exit Insert mode by pressing esc, and save and exit by typing :wq.
Issue the following commands to make the script executable, create the symlink, make the symlink executable, and check your work:
% sudo chmod +x /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
% sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink /usr/bin/jlink
% sudo chmod +x /usr/bin/jlink
% ls -la $(which jlink)
lrwxr-xr-x 1 root wheel 75B Jun 19 10:33 /usr/bin/jlink# -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jlink
% ls -la $(which java)
lrwxr-xr-x 1 root wheel 74B Sep 25 2017 /usr/bin/java# -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
% jlink --version
10.0.1
%
Now it's time to re-enable System Integrity Protection. Reboot back into Recovery Mode (instructions in step 1).
Once in Recovery Mode, choose Terminal from the Utilities menu.
At the prompt, type csr enable. You'll be prompted to reboot once more.
If you'd rather not muck with disabling/re-enabling SIP, you can just create the script at /usr/bin/jlink.
Hopefully some future version of macOS will include these by default.

Related

Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

I'm trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash.
What's at the end of my bashrc:
export JAVA_HOME="/mnt/d/Program Files/Java/jdk-11.0.1"
export PATH="/mnt/d/Program Files/Java/jdk-11.0.1/bin:$PATH"
CMD INPUT/OUTPUT:
C:\Users\jaall>javac --version
javac 11.0.1
BASH INPUT/OUTPUT:
myubuntu_name#DESKTOP-LUK3BII:~$ javac --version
Command 'javac' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless
I've been stuck on this for awhile and can't figure it out or find a working solution online. Thanks!
As Biswapriyo suggested, you should use WSLENV.
Open PowerShell. Then set JAVA_HOME to the path to your java installation.
In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"
You should see a message that says "SUCCESS: Specified value was saved".
Then run setx WSLENV "JAVA_HOME/p".
You should see the success message again.
Type 'env' into your WSL bash prompt.
You should see JAVA_HOME correctly set at this point.
Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the \bin folder.
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
There's a much easier way to make Windows and WSL utilize the same JavaSDK binary, you just need to set up a few things first. Best of all, if you have JavaSDK installed on Windows, you do not need to install Linux binaries.
Check WSL Permissions and Directory Link (Optional, but recommended)
In WSL, list symbolic links on PC:
ls -l /mnt
If any drive is owned by root, perform your WSL dev work in /mnt/c/Users/<UserName>
Personally, I create a development directory in Windows and add a symbolic link to the directory in WSL:
ln -s /mnt/d/dev/environment/ ~/dev
cd dev now brings you to your development directory.
Ensure Java for Windows works
Open PowerShell/cmd.exe from any directory and enter: java --version
You should get a list of JRE 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, but if you have a ./bash_profile or /etc/profile structure set up, you can put it there.
# Shared environment variables
# Use 'java.exe <args>' to utilize Windows Java binaries from within WSL.
export JAVA_HOME=/mnt/d/Java/jdk11.0.4_10
While we're at it, let's add Maven too:
export MAVEN_HOME=/mnt/d/software/apache-maven-3.6.2
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.
For instance, if in Windows, Java 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.
Windows PowerShell/cmd:
javac GetStringLength.java
java GetStringLength
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 originally had Maven working in Windows attempted to run Maven in WSL2 and tried all of the previous solutions, but would consistently get the following no matter what I set for JAVA_HOME and PATH:
$ mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
The issue was I was trying to use a Windows version of the JDK in the WSL2 Linux kernel. To fix this I ended up having to install a Linux version of the JDK (version 11) in the WSL as follows:
$ sudo apt update
$ sudo apt install openjdk-11-jdk
$ sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.
Then take the path for your JDK and use it to create JAVA_HOME and update PATH by appending the following in .profile
export PATH="/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH"
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Now close and reopen WSL2 and when your try again:
$ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.13, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.10.16.3-microsoft-standard-wsl2", arch: "amd64", family: "unix"
Since I've never been able to share variables between the 2 systems easily, I created a simple bash function which can easily retrieve (and define, if asked to) any Windows Environment variable.
It also takes care of paths so they get converted from Win32 to Un*x-like.
I added this to /etc/bash.bashrc:
winenv()
{
if [ "$#" == "0" ] || [ "$1" == "--help" ]
then
echo $'\n'Usage:
echo $'\t'winenv [-d] WINDOWS_ENVIRONEMENT_VARIABLE_NAME
echo $'\t'-d: Defines environment variable in current shell
echo $'\t Note that paths will be translated into un*x-like\n'
return
fi
local IFS='$\n'
local PATH_TO_TRANSLATE=$1
[ "$1" == "-d" ] && PATH_TO_TRANSLATE=$2
local VAR=$(cmd.exe /c echo %${PATH_TO_TRANSLATE}% | tr -d '\r')
local NEW=$(wslpath -u "${VAR}" 2>/dev/null || echo ${VAR})
echo "${PATH_TO_TRANSLATE} = ${VAR} -> ${NEW}"
[ "$1" == "-d" ] && export "${PATH_TO_TRANSLATE}=${NEW}"
}
And all I have to do to display one is to call winenv PROGRAMFILES (for example)
Or if I expect to export it, I just have to add a -d argument before the variable name as in winenv -d WINDIR.

can't avdmanager and other SDK tools with same java version [duplicate]

When installing the android sdk tools the following error is emitted:
java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
Why is this happening and how can it be fixed?
Debug output:
$ java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
$ brew cask install android-sdk
==> Caveats
We will install android-sdk-tools, platform-tools, and build-tools for you.
You can control android sdk packages via the sdkmanager command.
You may want to add to your profile:
'export ANDROID_SDK_ROOT=/usr/local/share/android-sdk'
This operation may take up to 10 minutes depending on your internet connection.
Please, be patient.
==> Satisfying dependencies
==> Downloading https://dl.google.com/android/repository/sdk-tools-darwin-3859397.zip
Already downloaded: /Users/tomasnovella/Library/Caches/Homebrew/Cask/android-sdk--3859397,26.0.1.zip
==> Verifying checksum for Cask android-sdk
==> Installing Cask android-sdk
==> Exception in thread "main"
==> java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
==> at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
==> at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
==> at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
==> at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:117)
==> at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:93)
==> Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
==> 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)
==> ... 5 more
Error: Command failed to execute!
==> Failed command:
/usr/local/Caskroom/android-sdk/3859397,26.0.1/tools/bin/sdkmanager tools platform-tools build-tools;26.0.1
==> Standard Output of failed command:
==> Standard Error of failed command:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:117)
Just had this error, solved by downloading the Android SDK Command-line Tools (latest) on Android Studio, under Preferences > Appearance & Behavior > System Settings > Android SDK > SDK Tools and re-running flutter doctor --android-licenses
Finally, add the new tools to your PATH, in your .bashrc, .zshrc or similar, before the obsolete tools:
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin
I had a similar problem this morning (trying to build for Android using Unity3D). I ended up uninstalling JDK9 and installing Java SE Development Kit 8u144.
brew cask uninstall java # uninstall java9
brew tap homebrew/cask-versions
brew cask install java8 # install java8
touch ~/.android/repositories.cfg # without this file, error will occur on next step
brew install --cask android-sdk
I also had this error
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 5 more
then instead of uninstalling the latest java environment, (in my case it is java 13)
and installation of java 8,
I have done the following steps
open the android studio > go to configure > select sdk manager > go to sdk tools > make a tick on android sdk command line tools >apply > and wait for installation
restart the command line tool
enter the command flutter doctor
enter the command flutter doctor --android-licenses
and accept all the licenses by typing y.
To solve this error, you can downgrade your Java version.
Or exports the following option on your terminal:
Linux/MAC:
export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
Windows:
set JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee
If this does not work try to exports the java.xml.bind instead.
Linux:
export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.xml.bind'
Windows:
set JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.xml.bind
And to save it permanently you can exports the JAVA_OPTS in your profile file on Linux (.zshrc, .bashrc and etc.) or add it as an environment variable permanently on Windows.
ps. This doesn't work for Java 11/11+, which doesn't have Java EE modules. For this option is a good idea, downgrade your Java version or wait for a Flutter update.
Ref: JDK 11: End of the road for Java EE modules
set JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee
This fixed the problem on Windows for me.
Source 1, source 2
Update 2019-10:
As stated in the issue tracker, Google has been working on a new Android SDK Command-line Tools release that runs on current JVMs (9, 10, 11+) and does not depend on deprecated JAXB EE modules!
You can download and use the new Android SDK Command-line Tools inside Android Studio or by manually downloading them from the Google servers:
SDK Tools for Linux
SDK Tools for Mac OS
SDK Tools for Windows
For the latest versions check the URLs inside the repository.xml.
If you manually unpack the command line tools, take care of placing them in a subfolder inside your $ANDROID_HOME (e.g. $ANDROID_HOME/cmdline-tools/...).
Update 2021-03:
The latest stable command-line tools are available at Googles Downloads-Website. These tools are newer than those linked above.
If you don't want to change your Java version (I don't), you can temporarily change the version in your shell:
First run
/usr/libexec/java_home -V
Then pick a major version if you have it installed, otherwise install it first:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now you can run sdkmanager.
When using Linux, an easy option is installation of JDK version 8 then selecting it as the default using:
sudo update-alternatives --config java
On Mac/Linux use the following command:
export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
Works for both JDK 9 and 10, without patching any script (sdkmanager, avdmanager).
For Java 11 see: https://stackoverflow.com/a/51644855/798165
You need to add the following to your Profile (Works on MacOS):
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
No need to patch anything.
While building the flutter application an error occurred saying android sdk licenses not accepted. So, while accepting the license this error occurred.
Reason for this error in my case was I haven't installed sdkmanager command line tools which is required to accept the license.
So, to install command line tools easiest way is:
Open android studio.
Open SDK Manager (refer image below):
Select SDK tools inside it in bar.
Tick the option Android SDK Command-line Tools (latest)
Apply it.
Run flutter doctor -android-licenses if using flutter or you can continue with the process where the error occurred.
I found two answers that worked for me, without having to uninstall JDK 10 (or 9), which I need for create-react-app. Both JDK 9 and 10 are incompatible with android-sdk !
Siu Ching Pong -Asuka Kenji- suggests modifying the sdkmanager script, replacing this line:
DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME"'
with:
DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
Note that this mod will be overwritten when updating sdkmanager.
Check out his post, and the one he links to, for more details.
This solution was also one of the solutions mentioned in this github issues thread.
German's post indicates the source of the conflict, and presents fix that will not not be overwritten by updates.
He suggests renaming /Library/Java/JavaVirtualMachines/Info.plist as a means of obscuring it from the script that looks for the highest version of Java that resides on your system. In this way, JDK 8 is returned as the default.
Referring to JDK 10 explicitly, or by setting it to $JAVA_HOME, you can use JDK 10 , instead of the default, whenever needed.
Details are in his post.
Strangely Java9 is not compatible with android-sdk
$ avdmanager
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
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)
... 5 more
Combined all commands into one for easy reference:
$ sudo rm -fr /Library/Java/JavaVirtualMachines/jdk-9*.jdk/
$ sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
$ sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
$ /usr/libexec/java_home -V
Unable to find any JVMs matching version "(null)".
Matching Java Virtual Machines (0):
Default Java Virtual Machines (0):
No Java runtime present, try --request to install
$ brew tap caskroom/versions
$ brew cask install java8
$ touch ~/.android/repositories.cfg
$ brew cask install android-sdk
$ echo 'export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"' >> ~/.bash_profile
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
$ avdmanager
Usage:
avdmanager [global options] [action] [action options]
Global options:
-s --silent : Silent mode, shows errors only.
-v --verbose : Verbose mode, shows errors, warnings and all messages.
--clear-cache: Clear the SDK Manager repository manifest cache.
-h --help : Help on a specific command.
Valid actions are composed of a verb and an optional direct object:
- list : Lists existing targets or virtual devices.
- list avd : Lists existing Android Virtual Devices.
- list target : Lists existing targets.
- list device : Lists existing devices.
- create avd : Creates a new Android Virtual Device.
- move avd : Moves or renames an Android Virtual Device.
- delete avd : Deletes an Android Virtual Device.
I faced the same problem. Though I am a little bit backdated developer (Still using windows to develop :P)
To solve this issue on windows :
STEP 1: Install jdk 8 if it wasn't installed (jdk 9 or 11 doesn't work but you may have them installed for using in other dev uses).
Very simple using Chocolatey:
choco install jdk8
(If installed using Chocolatey, skip steps 2 and 3)
STEP 2: Go to the Environment variables settings and set JAVA_HOME TO jdk 8's installation directory.
STEP 3: Go to path variable and add bin directory of jdk 8 and move it to top.
STEP 4: Close any open terminal sessions and restart a new session
OPTIONAL STEP 5: Depending on your objective in the terminal run (may need to add sdkmanager to path or just navigate to the directory):
sdkmanager --update
That's all! :O Enjoy fluttering! :D
Since Java 11 has removed JavaEE you'll need to download some jars and add to the classpath:
JAXB:
https://javaee.github.io/jaxb-v2/
JAF:
https://www.oracle.com/technetwork/articles/java/index-135046.html
Then edit sdkmanager.bat so that set CLASSPATH=... ends with ;%CLASSPATH%
Set CLASSPATH to include JAXB and JAF:
set CLASSPATH=jaxb-core.jar;jaxb-impl.jar;jaxb-api.jar;activation.jar
Then sdkmanager.bat will work.
I ran into same issue when running:
$ /Users/<username>/Library/Android/sdk/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"_
I solved it as
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
$ ls /Library/Java/JavaVirtualMachines/
jdk-11.0.1.jdk
jdk1.8.0_202.jdk
Change Java to use 1.8
$ export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home'
Then the same command runs fine
$ /Users/<username>/Library/Android/sdk/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"
In my case, I need both JDK 8 (trying to use the AVD and SDK manager in Qt under ubuntu) and 11 for different tools. Removing version 11 is not an option.
The 'JAVA_OPTS' solutions did not do anything. I don't really like the export JAVA_HOME, as it might force you do launch whatever tool calls these utils from the same shell (like Qt), or force you to make this permanent, which is not convenient.
So for me the solution is quite simple. Add something like this in the second line of ~/Android/tools/bin/sdkmanager and ~/Android/tools/bin/avdmanager:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
(or whatever the path is to your rev 8 jdk).
With this, these command line tools work in a stand alone mode, they work also when called by other tools such as Qt, and jdk 11 is still the system default for others. No need to mix libs etc...
The only downside is that any update to these command line tools will erase these modifications, which you will have to put back in.
As of the latest version of the Android command-line tools (2.1 as of this writing), it is no longer necessary to patch sdkmanager nor to downgrade to an ancient version of Java.
Simply update your SDK packages and switch your command-line tools to the latest release, which will track new versions as they come:
sdkmanager --update
sdkmanager 'cmdline-tools;latest'
sdkmanager --uninstall 'cmdline-tools;1.0'
You may need to provide the full path to sdkmanager if it's not on your PATH, and you may need to adjust your PATH afterwards if it was referring to the 1.0 tools explicitly.
In my case, I have Java 14 and need Java 8.
I'm in a Arch Linux and has installed jdk8-openjdk jre8-openjdk https://www.archlinux.org/packages/extra/x86_64/java8-openjdk/
For Debian users https://wiki.debian.org/Java, or Fedora https://docs.fedoraproject.org/en-US/quick-docs/installing-java/.
Install Java 8 (or desired version, in this case jdk8-openjdk jre8-openjdk) using your package manager before doing the following steps.
1. Figuring out where is my Java:
# which java
/usr/bin/java
2. Checking java files:
I can see all java files here are links to /usr/lib/jvm/default[something]. This means that the java command is linked to some specific version of java executable.
# ls -l /usr/bin/java*
lrwxrwxrwx 1 root root 37 May 16 06:30 /usr/bin/java -> /usr/lib/jvm/default-runtime/bin/java
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javac -> /usr/lib/jvm/default/bin/javac
lrwxrwxrwx 1 root root 32 May 16 06:30 /usr/bin/javadoc -> /usr/lib/jvm/default/bin/javadoc
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javah -> /usr/lib/jvm/default/bin/javah
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javap -> /usr/lib/jvm/default/bin/javap
3. Checking the default and default-runtime
Here I could see the default version was linked to 14 (unique installed version).
# cd /usr/lib/jvm
# ls -l
lrwxrwxrwx 1 root root 14 Aug 8 20:44 default -> java-14-openjdk
lrwxrwxrwx 1 root root 14 Aug 8 20:44 default-runtime -> java-14-openjdk
drwxr-xr-x 7 root root 4096 Jul 19 22:38 java-14-openjdk
drwxr-xr-x 6 root root 4096 Aug 8 20:42 java-8-openjdk
4. Switching the default version
First, remove the existing default and default-runtime which linked to java-14 version.
# rm default default-runtime
Then, create new links to the desired version (in this case, java-8).
# ln -s java-8-openjdk default
# ln -s java-8-openjdk default-runtime
The strategy is to make links to the desired version of software (java8 in this case) using ln -s above. Then, this links are linked to the binaries inside the java bin directory (without changing the $PATH environment variable!)
Or you might be wanted to change the Java version using archlinux-java command instead with more safely approach: https://wiki.archlinux.org/index.php/Java
TLDR; Try setting JAVA_HOME worked fine for me on OSX
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
To install the JDKs 8 ( LTS ) from AdoptOpenJDK:
# brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk/openjdk/adoptopenjdk8
This question has numerous answers, and they're all different due to users installing different toolchains and using different Java versions.
The recommended way of using Android development toolchain, or at least the one that I suggest to use, is to follow what's stated in Android Studio documentation:
You should always keep your Build Tools component updated by downloading the latest version using the Android SDK Manager.
Android studio allows you to easily manage installed SDKs & build tools, yes, it requires some space on your hard drive, but it will save you some time. Once you get familiar with how it works, then you can think of installing command-line tools only.
If there's no particular reason of using older Java version, use the latest (stable) version, you will have interesting new features, and also the compiled application will benefit from all the new optimizations.
Fresh install
Delete your local Android folder, usually in the home directory
Download Android studio
Once installed, open Settings, Search Android SDK and open it
In SDK Platforms select the target Android version for your app
In SDK Tools tab, select Android SDK Build-Tools, Android SDK Command-line Tools (latest), Android Emulator, Android SDK Platform-Tools
Before pressing OK, check that Android SDK Location path is correct for you
Press OK and let Android Studio download & install everything
(Optional) if you need to use the installed binaries from command line, be sure to add their folder into your PATH variable. If you use Android studio, it's should not be required though.
I've got Java 14 installed on my machine, anyway you can use the jre shipped with Android Studio.
For Linux users (I'm using a Debian Distro, Kali)
Here's how I resolved mine.
If you don't already have jdk-8, you want to get it at oracle's site
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
I got the jdk-8u191-linux-x64.tar.gz
Step 1 - Installing Java
Move and unpack it at a suitable location like so
$ mv jdk-8u191-linux-x64.tar.gz /suitablelocation/
$ tar -xzvf /suitablelocation/jdk-8u191-linux-x64.tar.gz
You should get an unzipped folder like jdk1.8.0_191
You can delete the tarball afterwards to conserve space
Step 2 - Setting up alternatives to the default java location
$ update-alternatives --install /usr/bin/java java /suitablelocation/jdk1.8.0_191/bin/java 1
$ update-alternatives --install /usr/bin/javac javac /suitablelocation/jdk1.8.0_191/bin/javac 1
Step 3 - Selecting your alternatives as default
$ update-alternatives --set java /suitablelocation/jdk1.8.0_191/bin/java
$ update-alternatives --set javac /suitablelocation/jdk1.8.0_191/bin/javac
Step 4 - Confirming default java version
$ java -version
Notes
In the original article here: https://forums.kali.org/showthread.php?41-Installing-Java-on-Kali-Linux,
the default plugin for mozilla was also set. I assume we don't really need the plugins as we're
simply trying to develop for android.
As in #spassvogel's answer, you should also place a #repositories.cfg file in your ~/.android directory
as this is needed to update the tools repo lists
Moving some things around may require root authority. Use sudo wisely.
For sdkmanager usage, see official guide: https://developer.android.com/studio/command-line/sdkmanager
In my case I didn't have the required sdk version installed on my machine.
So make sure that you have installed the sdk version which is given in the error.
Here you can navigate check & install via Android Studio.
For windows machine uninstall the JDK if its more than 1.8.172.
Install JDK 1.8.172
I was facing the same issue in windows 10 with java 10. I uninstalled the java 10 and installed java8 its working fine for me now :)
Run java -version and javac -version commands in a command line to make sure that they come from the same JDK (eg: version 1.8.0_181)
If not, you have to modify PATH variable so that it only points to a single JDK. If you are not sure how to, just uninstall all other Java instances except for Java 8 (Add/Remove Programs in Windows). As for today, both Unity and Android recommends that you use JDK 8.
With Java 8, it is not necessary to export java.se.ee module as shown in some of the other answers. You may also remove any JAVA_OPTS or other environment variables that you have set.
Downgrade your java version.Whatever system or ide.
Make sure java version is not higher than 8
In my case.I change the ide java verion.This solves my issue.
I had recently solved this problem by uninstalling the higher version of JDK and installing JDK 8. After installing the JDK you need to give the path. Then you need to open command prompt in "C:\Users\Milan Adhikari\AppData\Local\Android\Sdk\tools" and run "sdkmanager --update" which will update your sdk and then you need to run "flutter doctor --android-licenses" in cmd and accept all the licenses.
Now your problem should be solved.
Best way is to use below command
$ wget https://dl.google.com/android/repository/platform-tools-latest-linux.zip
$ unzip \platform-tools-latest-linux.zip
$ sudo cp platform-tools/adb /usr/bin/adb
$ sudo cp platform-tools/fastboot /usr/bin/fastboot
Now run adb version to verify it’s been updated.
No need to uninstall your other java version(s) that's already installed on your machine. Whenever required, you can conveniently use the utility 'update-alternatives' to choose the Java runtime that you wish to activate. It will automagically update the required symbolic links.
You just need to run the below command and select the version of your choice. That's all!
sudo update-alternatives --config java
As #steven pointed out, install Java 8 (here a link for Ubuntu 16.04, 18.04 and 20.04/20.10 https://computingforgeeks.com/how-to-install-java-8-on-ubuntu/) and then set it as the default Java version with this command:
sudo update-alternatives --config java

Hadoop Installation 2.6.0 on Ubuntu 14 - Java Error

EDIT
I am trying to install Hadoop 2.6.0 on my Ubuntu 14 machine. I am coming across an error though.
When I am trying to set the HOME variable for Java it does not seem to be doing as expected.
I am on my machine as hduser setup specifically for running and using Hadoop. This user is a sudoer.
Some information:
java -version' gives the following
java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
This is the only version installed on my machine, which can be seen by running the following command:
update-alternatives --display java
Which gives the following message:
java - auto mode
link currently points to /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java - priority 1071
slave java.1.gz: /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java'.
I then go to the following path:
cd /usr/lib/jvm
and the I list out the contents ls
default-java java-1.7.0-openjdk-amd64 java-7-openjdk-amd64
I then type cd java* and pwd which brings up the following path:
/usr/lib/jvm/java-1.7.0-openjdk-amd64
Ok, so with that information, I then copy that directory into the .bashrc file as follows:
# The java implementation to use.
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
The hadoop-env.sh file I fill out as follows:
#Hadoop variables
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
I then at the terminal type source ~/.bashrc and then restart the terminal in order for it to set to the new Java path. When typing Hadoop -version I get the following output:
/usr/bin/hadoop: line 350: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
/usr/bin/hadoop: line 434: /usr/lib/jvm/java-6-sun/bin/java: No such file or directory
I do not know where to go from here.
Thank you,
Add JAVA_HOME to point to your openjdk in hadoop-env.sh. Add this line in hadoop-env.sh:
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
NOTE: Change JAVA_HOME path in .bashrc too
UPDATE I:
Run these commands in terminal. (This will set java & javac in /bin to use your jdk)
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java 1
sudo update-alternatives --config java
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac" 1
sudo update-alternatives --config javac
NOTE: If you dont have java and javac in the specified path, it will be inside /jre folder. Change it respectively.
It seems that you have installed java (I mean JRE/JDK here) 1.6, 1.7 and 1.8 at the same time. Which is comlpetelly ok since java installations are usually managed via alternatives subsystem.
The problem is that (contrary to your expectations) setting JAVA_HOME doesn't select which java is used. This is done via mentioned alternatives subsystem instead. The JAVA_HOME variable itself is only addtional configuration and doesn't have power to override what is being executed when one asks to start java process.
Moreover running cd java* isn't really a good idea unless you would like to go into first directory which starts with given string. Try to run ls java* to see my point. And again, it's ok to have multiple different versions of java here thanks to alternatives subsystem.
Another problem is that you are mixing different flavours of java which you probably don't have installed (java-8-oracle vs java-6-sun vs java-1.6.0-openjdk.
To check which java is installed, run:
alternatives --display java
And then based on the results, swich to one version of java and set JAVA_HOME accordingly.

Find JAVA_HOME and set it on RHEL

I had installed java a while ago on my RHEL machine. Now, I'm trying to run a program that requires the JAVA_HOME variable to be set. What is the best way to figure out the installation directory of my java installation and then set JAVA_HOME? Here are the results of running java- version:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
I have a /usr/lib/jvm directory, but it is empty.
RHEL uses alternatives subsystem to manage java installations. You can have
multiple versions of java installed, but only one is active at a time.
This means that running which java doesn't provide useful information. The
output would be the same no matter which java installation is selected via
alternatives. Running readlink -f $(which java) (as already suggested in
other comment) or using asking alternatives alternatives --display java would
be better.
See example from RHEL 6 machine with OpenJDK installed (which is shipped with
RHEL):
[root#example ~]# which java
/usr/bin/java
[root#example ~]# readlink -f $(which java)
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64/jre/bin/java
[root#example ~]# alternatives --display java | head -2
java - status is manual.
link currently points to /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
Note that enviroment variable JAVA_HOME is not defined anywhere by default,
you would need to define it yourself in .bashrc of user which requires it.
In previous example, correct value of JAVA_HOME would be
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64.
See details in Install OpenJDK
documentation, search for section "Optional: Set the JAVA_HOME environment variable".
First, try echo $JAVA_HOME from the command line. Since java is on your path already, JAVA_HOME may be set.
What is the best way to figure out the installation directory of my java installation
Running the command which java will point you to where java is installed.
and then set JAVA_HOME
You can edit ~/.bashrc, ~/.bash_profile, or /etc/profile to set JAVA_HOME. Setting it in ~/etc/profile will set it system wide, and this is probably not what you want. Say for the sake of example the output of which java is /opt/jdk_1.7.0_25, then you'd just add export JAVA_HOME=/opt/jdk_1.7.0_25 to ~/.bashrc or ~/.bash_profile and then run source ~/.bashrc (or source ~/.bash_profile if you set it there).
Note that in this case, java is on the PATH but in some cases you'd need to add export PATH=$PATH:$JAVA_HOME/bin to add the JAVA_HOME variable to the PATH.
readlink command will show you full path of symbolic link:
readlink -f `which java`
The best you can do is avoid Red Hat's java altogether.
Get your java from Oracle and put it in /opt.
Then just create symlink /opt/java -> /opt/jdk-someversion, and create /etc/profile.d/java.sh containing
#!/bin/sh
export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH
Then, to change system-wide java, just change symlink in opt.
To use multiple java versions, use scripts like the above with appropriate JAVA_HOME.
Furthermore, /sbin/service script used to run /etc/init.d scripts will rip off environment variables - executes env -i explicitly. So i.e. your tomcat will not get JAVA_HOME, you'll have to create setenv.sh in $CATALINA_BASE/bin.
Drawback to this approach is you don't get java updates from Red Hat.
I found a way to combine the above so that I can programmaticly set JAVA_HOME.
export JAVA_HOME=$(dirname $(readlink -f $(which java))|sed 's^jre/bin^^')
On Ubuntu, returns /usr/lib/jvm/java-8-openjdk-amd64/.
On CentOS7, returns /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/.
This can be added to a Makefile with
JAVA_HOME = $(shell dirname $$(readlink -f $$(which java))|sed 's^jre/bin^^')
This is much safer than exporting a hardcoded path and is more portable.
At least on RHEL 7, alternatives sets up a slave link for java_sdk at:
/etc/alternatives/java_sdk/
This is a symlink to the root of the SDK installation.
For me worked to change link in
/etc/alternatives for:
java -> /app/java/jdk8u222-b10/jre/bin/java
I know this is old java, but this server was neglected in our company.

Java installation issues on Ubuntu

Trying to install Java (JDK 6) on my new Ubuntu system and getting some bizarro errors. This is my first time ever using any flavor of Linux and so I'm sure it's a user issue (permissions or otherwise).
I downloaded the BIN file directly off Oracle's site (Java SE 6u23 for 64-bit Linux). This defaulted to downloading to /home/myUserName/Downloads.
From there I moved the file to /opt/java, which was a directory I created, because (as a Linux novice) that made sense to be the directory where Java should go.
I then ran the following 2 commands, per instruction I found online for running BINs:
chmod +x jdk-6u23-linux-x64.bin
sudo ./jdk-6u23-linux-x64.bin
Now, in my /opt/java directory I see both the BIN file and the jdk1.6.0_23 directory that seems to be intact upon inspection.
But, when I open a new terminal and run java -version, I get:
The program 'java' can be found in the following packages:
- gcj-4.4-jre-headless
- gcj-4.5-jre-headless
- openjdk-6-jre-headless
Try: sudo apt-get install
What is going on here?!?
(1) Was I wrong to try and make /opt/java my Java directory?
(2) Did I run the wrong commands?
(3) Is Java 1.6.0_23 even installed on my machine?
(4) What are all those gcj-xxx-headless targets?!?!
Thanks for any input!
Was I wrong to try and make /opt/java my Java directory?
Not really. Many Java developers install multiple JDK installations and always use /opt/jdk1.6.0_23 or similar paths. The bin file you downloaded is not an installer, but merely an extractor. It does not install the java binaries into system folders like /bin.
I usually download the JDK and execute it from within my home folder and afterwards move it to /opt and performing an chown.
Did I run the wrong commands?
Not really. In case you wanted to install a separate JDK, you did it correctly. In case you wanted system integration, you would be better off to use the distribution-specific packages, such as the one installed via aptitude install sun-java6-jdk or alike.
The bin you downloaded is imho more flexible, since I can use it to install multiple verisons of Java on the same system. I know this is something you don't often do on Linux machines.
If you want to use the java binary on command line, you'd have to manually set up the PATH and JAVA_HOME environment variables. I think on Ubuntu that's /etc/environment or /etc/profile or something like that.
Is Java 1.6.0_23 even installed on my machine?
Not really. See above answers.
What are all those gcj-xxx-headless targets?!?
The GCJ is the Gnu Compiler for Java. Obviously, it includes a Java Development Kit and a Java Runtime Environment.
Why downloading a bin, when you can simply:
sudo apt-get install sun-java6-jdk
If there isn't any special reason why you'd want that specific version from the site, you should use apt-get because it will take care of all the stuff like PATH variable, etc.
Please follow below steps to install oracle java:
Download the latest Java SE SDK version.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Untar the Archive:
tar -xzvf jdk-8-linux-x64.tar.gz
mv jdk1.8.0 /opt
cd /opt/jdk1.8.0
This step registers the downloaded version of Java as an alternative, and switches it to be used as the default:
update-alternatives --install /usr/bin/java java /opt/jdk1.8.0/bin/java 1
update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0/bin/javac 1
update-alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /opt/jdk1.8.0/jre/lib/amd64/libnpjp2.so 1
update-alternatives --set java /opt/jdk1.8.0/bin/java
update-alternatives --set javac /opt/jdk1.8.0/bin/javac
update-alternatives --set mozilla-javaplugin.so /opt/jdk1.8.0/jre/lib/amd64/libnpjp2.so
Test
To check the version of Java you are now running
java -version
Output
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
To check the browser plugin browse to http://www.java.com/ and click “Do I have Java?”
Ref: https://askubuntu.com/questions/437776/ubuntu-13-04-unable-to-install-jdk7
You simply have put the JDK binaries in a directory. Although by convention /opt/java or /opt/jdk is often used, these are not directories that are automatically recognized by the system.
You can however update your PATH environment variable to include the /opt/java/bin dir, or symlink (ln -s) /opt/java/bin/java in one of the directories on your system that are included in your path like /usr/bin/
The JDK you installed from Sun/Oracle is the original JDK. The "headless" JDK is the open source alternative.
When you run the JDK BIN file, it simply extracts the archive. When you entered the java -version command, it found the FOSS Java, not the Java you had extracted in /opt. As somebody else had mentioned, developers keep multiple versions of the JDK. If you wish to use the Oracle's Java, then you need link /usr/bin/java to /opt/jdk1.6.0_23/bin/java.
sudo ln -s /usr/bin/java /opt/jdk1.6.0_23/bin/java
For this to work, the existing java command should be first delinked from the "headless" JDK. (Do the following before the previous command.)
sudo mv /usr/bin/java /usr/bin/java_old
This assumes that there is a link or executable named java in /usr/bin. Use the which command to be sure.
which java
To add a new pathname to the existing PATH variable, you need to type this in Terminal:
PATH=`echo $path`:/your/new/path
export PATH
If you had lost your original PATH variable, you could restore by entering this:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
export PATH
Try:
rm -rf /usr/bin/javac
rm -rf /usr/bin/jar
ln -s /home/jdk1.6.0_13/bin/javac /usr/bin/javac
ln -s /home/jdk1.6.0_13/bin/jar /usr/bin/jar
This way, your linux can find java && javac in /usr/bin

Categories