How to get the JDK path in Gradle within Android Studio? - java

In order to build some Android projects, it's necessary to set the environment variable JAVA_HOME. (See this Stack Exchange question and flutter bug report.)
This is the case for a project I'm working on. I would like to change the Gradle file so it is not necessary to set JAVA_HOME.
Is there any way for Gradle to get the path to the JDK used by the enclosing Android Studio process (or for the process to pass in the JDK without user intervention)? This should work when JAVA_HOME had not been set.

you can define a task in your_project_path/app/build.gradle
task javaHome {
println "JAVA_HOME:" + System.getProperty("java.home")
}
run task:
./gradlew -q app:javaHome
RESULT:
JAVA_HOME:/usr/lib/jvm/java-8-openjdk-amd64/jre

There are generally two options available:
a) Edit the gradle.properties file and define which JDK you want to use:
org.gradle.java.home=(path to JDK home)
There it cannot be set dynamically, because it's an egg/hen problem.
And it might also fail on other computers, because the path may vary.
But one can pass it as command-line option:
./gradlew -Dorg.gradle.java.home=$JAVA_PATH
For reference: Build Environment.
b) However, one can also add an export JDK_HOME statement on top of file gradlew. Came up with a shell script, which can at least detect the default JDK install on Linux (Android Studio runs on JRE):
tmp=`which java`
export JAVA_HOME=${tmp::-9}
echo $JAVA_HOME
/usr/java/jdk1.8.0_172
Of course, one also would have to consider no JDK being present at all:
/usr/bin/which: no java in ...
Generally, this assumes a default JDK had been set with alternatives, as a package manger usually would do; eg yum install jdk1.8.0_102.x86_64. The problem here is, that there is no easy way to identify which JDK path to use on Windows, because Windows has no which command and one would likely have to read from the registry. A helper PS script or executable could look up the value and truncate as required, called from file gradlew.bat. There still may be other ways to get the path.

Related

Install java Open JDK 10 on AWS linux

How do I setup https://jdk.java.net/10/ so Solr 7.5 can find it under the installation?
I am using linux Amazon Linux 2 AMI 2.0.20181008 x86_64 HVM gp2.
Questions
In what directory should I put the files. ex. /usr/local/java?
Where should I set JAVA_HOME so every user will get it also after a reboot.
Where should I set the path to java so every user will get it also after a reboot.
Any installation location should work as long as you set the correct JAVA_HOME path and add the binary path to your $PATH variable.
You can configure both globally by editing /etc/environment, which should take effect for all users after restarting your shell (you can shortcut that process by doing source /etc/environment when testing).
JAVA_HOME=/usr/local/java/jre_10....
There should already be a PATH entry there which you can modify to add the Java binary directory.
This can differ between different Linux distributions and shell versions. If your distribution (Amazon Linux used it earlier at least) doesn't support it, you can use /etc/profile.
Another option that many prefer is to add an executable shell script in /etc/profile.d/ that sets up the variables for you, as these scripts are invoked on each initialization of a shell for all users.

java how to check version being used by 'jar launcher.app' on Mac OS

I have several .jar files that run successfully when opened with Mac's built in 'Jar Launcher' (Version 15.0.1) but when ran from terminal they error out.
Looking online the error messages might be down to the incorrect version of Java being linked to the terminal version, so I'd like to check if the options set for 'Jar Launcher' and the Java used within terminal are the same.
Does there exist any way to do this?
This question is two years old, but as there is no answer to it, I thought I'd put my two cents here since I've had issues myself, and have made some observations.
It seems that the Jar Launcher doesn't necessarily use the same JVM version as the one shown by the command /usr/libexec/java_home: If there is a JRE /Library/Internet Plug-Ins/JavaAppletPlugin.plugin, then the Jar Launcher will use it and ignore anything in /Library/Java/JavaVirtualMachines.
I've solve my problem of version mismatch between the Jar Launcher and the terminal by removing the JRE:
Go to /Library/Internet Plug-Ins.
Remove the JavaAppletPlugin.plugin directory by executing the rm command as a root user or by using the sudo tool.
Go to /Library/PreferencePanes.
Remove JavaControlPanel.prefpane by executing the rm command as a root user or by using the sudo tool.
Source: https://docs.oracle.com/javase/9/install/installation-jdk-and-jre-macos.htm

Ant is unable to find a javac compiler

I am trying to build some binaries of an application. When i am trying to make the build, I am getting this:
BUILD FAILED
/home/.../ant-build.xml:84: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
It is currently set to "/usr/lib/jvm/java-8-openjdk-amd64/jre"
The application requires java 7, and i have it installed but i had it along java 8. The build requires for tools.jar which is located in java 7 only, therefore after several attempts i uninstalled java 8. However, when I am trying make again, i get the same error, even though i don't have java8 installed at all!
I set the JAVA_HOME properly:
$ echo $JAVA_HOME
/usr/lib/jvm/java-7-openjdk-amd64/
I doubt i need the path but i set that one up just in case as well :
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-7-openjdk-amd64/bin
I'm running out of ideas here :P any help would be appreciated.
Also something maybe important, I had to add the repo manually to get java-7 otherwise it wasnt able to locate the package. Thats when i got double jdk-s.
I guess you are calling the JAVAC command from any other source/tool like Jenkins/Bamboo/Teamcity. Make sure you have set the environment variable of that tool. usually it lies in the manage tool option.
I was facing same issue while using Jenkins.If you don't set up an environment variable in tool from which you are making a call it take your
"/usr/lib/jvm/java-8-openjdk-amd64/jre" so go and set the the env variable JAVA_HOME of you tools. attaching screen shot of Jenkins issue. Go to Manage Jenkins -->configure system --> environment variables and set you java_home path.
Cheers
I just encountered the same situation.
That is because JAVA_HOME is not correctly set in sudo environment.
I added export JAVA_HOME=/path/to in /root/.bashrc, and eventually it was built right.
It seems that you are using javac of java8 with JAVA_HOME or CLASS_PATH pointing to java7.
You can use which java to check that.
export JAVA_HOME=/lib/jvm/java-7-openjdk-amd64
export export ${PATH}=${JAVA_HOME}:/bin:${PATH}`

jnetpcap library unable to build on ubuntu 12.04

I am trying to install JNetPcap and followed the instructions given at here. At step 12, I am unable to run the ant command and i see the error
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-6-sun/bin/java
As I am able to run Java classes from eclipse or from command line I don't think if it's a problem with JAVA_HOME.
echo $PATH shows
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/java-6-sun/bin
Kindly let me know if am missing something here.
thanks in advance
Neither running java from the command line or running eclipse will require JAVA_HOME to be set. However, the build procedure you are trying to use ant, and ant often does require JAVA_HOME to be set appropriately. (It actually depends on the version of ant that you are using. The use of JAVA_HOME is typically in the wrapper script for ant.)
Just set it.
JAVA_HOME should probably be set to /usr/lib/jvm/java-6-sun ... based on what you gave said PATH to.
However, it is also possible that the problem is that your PATH is incorrect. Or that you have (somehow) managed to get the owner/group/permissions on your Java install incorrect, such that the java command isn't executable.
Check that running java -version displays the installed Java version.
Repeat with /usr/lib/jvm/java-6-sun/bin/java -version.

How to install Apache Ant?

I want to install SMSlib (http://smslib.org/) in installation instruction (http://code.google.com/p/smslib/wiki/Installation).
In here I must install Apache Ant, but I didn't understand how to do that.
I already download Apache Ant 1.7.1 , read manual (http://ant.apache.org/manual/index.html)
In here I must have RPM version from jpackage.org right? I already go to http://www.jpackage.org/ after that what must I do? I already try (http://mirrors.dotsrc.org/jpackage/1.7/generic/free/repodata/) and (http://www.jpackage.org/browser/browse.php?jppversion=1.7) but too many link. I don't know witch one must I download
If you're on Windows, you can use WinAnt, a Windows installer for Apache Ant that I made.
Step 1: Download and install
Download Ant. Go to the Ant homepage and click to download the binary. Because we’re talking about Windows, choose to download the ZIP file rather than any of the others. Scroll down to where it says “Current release of Ant” and click on the ZIP filename.
Once downloaded, unzip the file. You’ll now need to choose a permanent home for Ant on the computer. c:\java\ant is often used, but you can put it wherever you want.
Step 2: Set environment variables
For Windows XP: To set environment variables on Windows XP, right click on My Computer and select Properties. Then go to the Advanced tab and click the Environment Variables button at the bottom.
For Windows 7: To set environment variables on Windows 7, right click on Computer and select Properties. Click on Advanced System Settings and click the Environment Variables button at the bottom.
The only environment variable that you absolutely need is JAVA_HOME, which tells Ant the location of your JRE.
If you’ve installed the JDK, this is likely
c:\Program Files\Java\jdk1.x.x\jre
on Windows XP and
c:\Program Files(x86)\Java\jdk1.x.x\jre
on Windows 7. You’ll note that both have spaces in their paths, which causes a problem. You need to use the mangled name[3] instead of the complete name. So for Windows XP, use C:\Progra~1\Java\jdk1.x.x\jre and for Windows 7, use C:\Progra~2\Java\jdk1.6.0_26\jre if it’s installed in the Program Files(x86) folder
That alone is enough to get Ant to work, but for convenience, it’s a good idea to add the Ant binary path to the PATH variable.
This variable is a semicolon-delimited list of directories to search for executables. To be able to run ant in any directory, Windows needs to know both the location for the ant binary and for the java binary. You’ll need to add both of these to the end of the PATH variable. For Windows XP, you’ll likely add something like this:
;c:\java\ant\bin;C:\Progra~1\Java\jdk1.x.x\jre\bin
For Windows 7, it will look something like this:
;c:\java\ant\bin;C:\Progra~2\Java\jdk1.x.x\jre\bin
Done
Once you’ve done that and applied the changes, you’ll need to open a new command prompt to see if the variables are set properly. You should be able to simply run ant and see something like this:
Buildfile: build.xml does not exist!
Build failed
That means Ant is installed properly and is looking for a build.xml file.
Source: http://www.nczonline.net/blog/2012/04/12/how-to-install-apache-ant-on-windows/
You can follow these instrunctions-
1)Download the latest version of ant from http://ant.apache.org/bindownload.cgi
2)Unzip and save it to your C:\ directory as ant.
3)Add the bin directory to your PATH environment variable.
4)Add the ANT_HOME environment variable set to C:\ant.
you can use following commands to set ANT_HOME variable
C:>set ANT_HOME=C:\ant
C:>set JAVA_HOME=C:\jdk
C:>set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin
C:>ant -version
Apache Ant version 1.8.1 compiled on April 30 2010
if you have java installed on your machine
5)Add the ANT_OPTS environment variable set to -Xmx256M.
This is all you need to install ant on your machine.
1) Download "apache-ant-1.8.4-bin.zip" from "http://ant.apache.org/bindownload.cgi"
2) Unzip it and copy "apache-ant-1.8.4" in "c:\Program Files"
3) Right Click "My Computer" -> properties -> Advanced -> Environment variables -> Edit variable "PATH" and append value "C:\Program Files\Java\jdk 1.7.0_04\bin;C:\Program Files\apache-ant-1.8.4\bin" and Click "OK".
4) open cmd and type "ant" for checking.
the installation of ant and smslib is pretty simple. All you need to do is extract it to some directory. Export the path in case of Linux or set the ANT_HOME\bin to the PATH variable to access it from any directory in the shell. ANT_HOME is the root directory where ant is installed.
For SMSLib Download the zip file and this link should guide you on a step by step basis
Sound's like you're using Linux. If so, the easiest way would be to use a package management frontend like Synaptic and install ant in there.
Its very simple just install Apache ant installer for windows give on [link][1]
[1]: http://code.google.com/p/winant/ and all is set.No need to set Environment variables.It will automatically do every thing for you.
You can install Apache Ant with only single command on Ubuntu:
sudo apt install ant
Check the version of ant installed with:
ant -version

Categories

Resources