I'm compiling a JNI library with Gradle. It clearly knows how to find a JDK (as the Java half of the project compiles perfectly). However, I'd need a path to the JNI header files in a Gradle native (C++) project.
Is there a way to obtain the path of the JDK being used from a Gradle build file?
According to the top reply here, you can get currently running Java path from the property java.home. In Gradle:
println System.getProperty('java.home')
or more Groovy:
println System.properties.'java.home'
Since this is the top answer on Google:
Jvm.current().getJavaHome()
Note that the JVM class is internal as of Gradle 6, use it at your own risk.
Related
I am using logback-classic 1.3.5 in my project but it is giving me UnsupportedOperationException. My project is on JDK 8 and when I saw in manifest file of logback-classic-1.3.5, it shows build-jdk-spec version as 19. I just want to know what exactly this "build-jdk-spec" version specifies
The build-jdk-spec attribute is a "default" attribute added by the Maven "maven-archiver-plugin" (reference). The value is the value of the ${java.specification.version} property for the build. It is (I think) the JDK version of the JVM that was running the build tool (Maven).
The [build-jdk-spec] version number most likely doesn't signify anything that is relevant to your problem. (It is NOT the the target Java version for the build!!)
The real cause of your problem can be deduced by looking at the details of the UnsupportedOperationException stacktrace.
I'm moving some libs to openjdk11 using the third approach described here.
The whole project is being compiled into 8, while only module-info.class to 11.
In the process of doing this, I found out that writing an empty main somewhere and running it, is the quickest way to check if there are any missing dependencies, the module-info.java has been properly detected and configured (because apparently gradle build is not that reliable).
I already did this for glm, which has two modules, decleared in the same build.gradle
Now I'm testing uno, but unfortunately I keep getting this error
Error:(1, 1) java: modules are not supported in -source 8
(use -source 9 or higher to enable modules)
My question is, why do I keep this error, when glm works fine?
I'm having some troubles trying to deploy a JavaFX application. In order to simplify my problem I've tried to do the same with a "Hello word" application and the problem is the same.
I'm currently using IntelliJ IDEA and Gradle.
My build.gradle file is this:
apply plugin: 'java'
apply from: "http://dl.bintray.com/content/shemnon/javafx-gradle/8.1.1/javafx.plugin"
repositories {
mavenCentral()
}
javafx {
mainClass 'Main'
}
That build.gradle file works. The problem is that it embeds the JRE into the bundle so the file size is about 175 MB. That's too much for a simple "Hello World" app, don't you think?
So, I want to bundle this simple app without the JRE (yes, I know that I should distribute my app with the JRE bundled so it doesn't relay on uses system but I'm going to distribute both versions: with and without JRE bundled). In order to do this I add a single line to the build.gradle file (as explained in this link:
...
javafx {
mainClass 'Main'
javaRuntime '<NO RUNTIME>'
}
But no bundles are generated when gradle jfxDeploy. In fact, running gradle jfxDeploy -i show some interesting info:
Java runtime to be bundled: none, bundle will rely on locally installed runtimes
...
Skipping Mac Application Image because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping DMG Installer because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping PKG Installer because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping Mac App Store Ready Bundler because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Ok, so maybe the plugin has some bugs. I try to generate the bundle with javapackager. I go to project folder and run the following:
javapackager -deploy -native image -srcfiles build/libs/ -outdir build/distributions -outfile Sample -appclass Main
The output is OK. The bundle is correctly generated with the JRE embedded. Now I try to generate a bundle without the JRE with this:
javapackager -deploy -native image -srcfiles build/libs/ -outdir build/distributions -outfile Sample -appclass Main -Bruntime=
(It's the same command appending -Bruntime= as explained in this link).
The bundle is generated. Now its size is about 500 KB. But when I try to run it nothing happens. Running it in a terminal gives the following (simplified) output:
$ Main.app/Contents/MacOS/Main
Failed to find library.:
Main:Failed to locate JNI_CreateJavaVM
Main:Failed to launch JVM
It seems that the bundle is not capable to start the local JVM. The jar is correctly generated and added to the bundle. Running it with java -jar runs the app but I don't know why it doesn't work when running the bundle
FYI, I'm running java 1.8.0_74, javac 1.8.0_74 and javapackager 8.0 in an OS X 10.11.2
The javafx.plugin from shemnon isn't developed nor maintained anymore, for that reason I've created the javafx-gradle-plugin.
The problem comes with the internal changes of the .cfg-file-format, they use INI-files now, but that is flawed in term of RUNTIME-configuration.
Official JDK-bug-tickets reported by me:
(jdk 9) https://bugs.openjdk.java.net/browse/JDK-8143314
(jdk 8) https://bugs.openjdk.java.net/browse/JDK-8143934
It should be enough to set some bundler-argument launcher-cfg-format to cfg within your build (or use the javafx-gradle-plugin, which includes that workaround automatically).
Disclaimer: I'm the maintainer of the javafx-maven-plugin and the creator and maintainer of the javafx-gradle-plugin.
UPDATE this got fixed and made available with JDK 8u92
I want specify the Java home path during building my Android gradle via command line; for example,
gradle build -d path of jdk
Is it possible?
According to gradle documentation:
The following properties can be used to configure the Gradle build
environment:
...
org.gradle.java.home Specifies the Java home for the Gradle build
process. The value can be set to either a jdk or jre location,
however, depending on what your build does, jdk is safer. A reasonable
default is used if the setting is unspecified.
org.gradle.jvmargs Specifies the jvmargs used for the daemon process.
The setting is particularly useful for tweaking memory settings. At
the moment the default settings are pretty generous with regards to
memory.
In other words, you can do it simply by running
gradle build -Dorg.gradle.java.home=<java home path>
Depending on what you want to accomplish, one of the following should work.
As Amnon Shochot suggested, set the -Dorg.gradle.java.home flag. This is probably preferable in most cases.
If you want to have use a particular JDK throughout, set the JAVA_HOME variable appropriately before executing gradle.
$ export JAVA_HOME=/usr/local/specialJava/
$ gradle build
If you don't want to change the environment, try adding the below to your build.gradle script. It should affect only the compiler used to compile Java code, nothing else. So Gradle doesn't run inside this particular JDK, but it will use it for compiling.
tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.executable = "/usr/local/specialJava/bin/javac"
}
(Last option stolen from here)
My work project needs to be compiled and run under JDK1.5 and I'm on a Mac. I followed the instructions here to get 1.5 back on Snow Leopard, and it works fine when building from IntelliJ IDEA, or if I'm just in the same directory as the build.xml and try "ant CleanRebuild" When I "Run Target" in NetBeans they're all compiled with the wrong version resulting in
java.lang.UnsupportedClassVersionError:
Bad version number in .class file
(unable to load class...
when tomcat is trying to start up.
So things I've tried
Set the "Java Platform" to 1.5 under the project properties/libraries.
Set the Source/Binary Format to JDK 5 under project properties/sources.
Pointed the ant home to the ant I'm using under preferences/ant
Renaming every javac executable I could find in the hopes NetBeans would fail to compile and I could figure out which one it was using (no luck)
Setting 1.5 as the default, resulting in the need to point $netbeans_jdkhome to the 1.6 jdk in order for NetBeans to even start.
All unsuccessful....
Again, if I cd into the directory of the netbeans project with the build.xml and run the command manually all is well....so NetBeans. What's the deal?
Revised answer
Assumptions: NetBeans version 6.9.1 (although likely applicable to most or all 6.x versions), alternative build systems (e.g., Maven) are not used...the default (Ant) is used.
NetBeans, by default, uses Ant as its build system for doing things like compiling a project, building a project, cleaning built files from a project, etc. Ant has two concepts that are applicable here: targets and tasks. A target, in Ant's vocabulary, is simply a "command" or a series of jobs that need to be completed for a particular job. In NetBeans, common targets are "Compile", "Build", "Clean and Build", etc. The "jobs" that a target completes are (among other things) Ant tasks. In NetBeans one task (which is particularly relevant in answering this question) is the Javac Task. This is the task that Ant uses to compile .java files into .class files.
An Ant-based project, and therefore a NetBeans project, uses the file build.xml to control the build process and tell Ant how to go about accomplishing the targets. In a NetBeans project, the build.xml is found in the root directory of the project, by default. NetBeans, however, uses a user-extensible build.xml file. The core targets and tasks defined by NetBeans are actually located in nbprojects/build-impl.xml and imported into build.xml within the first few lines of the file. The theory is that users can add or override things in build.xml while the core NetBeans-defined configuration remains untouched in the build-impl.xml file.
If you look in the default nbproject/build-impl.xml file for a NetBeans Java project, you will find the Javac task referred to twice. (Search for "<javac".) Both are in macro definitions, and therefore deep within the complexities of NetBean's default build configuration. If we refer to the Javac Task documentation we find that the tasks uses the compiler in the location specified either by the global build.compiler property, by the compiler attribute specified with the <javac... /> task, or the default which is the Java compiler that is used when running and, and thus the one that is used when running NetBeans (because it is what fires off the Ant process). Since we don't see build.compiler or the compiler attribute anywhere (in the default build-impl.xml), then we can only conclude that the default is being used.
So here we have the (more-or-less correct) first answer. NetBeans compiles using the JDK that was used to execute NetBeans by default. It looks like it is actually a bit more complicated than that simple answer, but it is essentially correct. If you look at the documentation for the Javac Task it alludes to "a class that implements the CompilerAdapter interface", which suggests that rather than calling the javac executable directly, Ant (and therefore NetBeans) compiles using the compiler class (that, in all likelihood, the javac executable also uses). Refer to the Original answer below to determine which JDK what used to run NetBeans.
So, what if you don't want to use the default JDK that was used to run NetBeans? This is where "Java Platforms" comes in. Go to the Tools menu, and click on "Java Platforms". You likely only have one platform defined here. (As an aside, this is actually the most correct answer to what JDK is used by default... the one defined here in the Java Platform Manager.) If you would like to compile against another Java version (say your default JDK is 1.6, but you want to compile against 1.5) then you would install the alternate JDK somewhere on your system, and then configure a platform here in NetBeans' Java Platform Manager. (I'll leave it as an exercise for you to find the documentation on how to add a Java Platform. A superficial search of the wiki didn't turn up anything obvious. In any case, it's fairly self-explanatory.)
Once a new platform is created in the manager, you would right-click on your project in the Projects tab, click on "Properties", and then on "Libraries". At the top, you would select the appropriate Java platform for the project. As soon as you change this value and click on "OK", NetBeans makes several adjustments to your build-impl.xml file that point it to the new JDK against which to compile. (It is instructive for the truly geeky amongst us to make a copy of the nbproject directory before making this change and to diff that against the new contents of the nbproject directory after the change is made.) The changes instruct the Javac Ant Task to use the (equivalent of the) javac executable of the specified platform. So here we have the most correct answer: NetBeans uses the equivalent of the javac executable (as invoked by the Ant javac task) that is specified in the project's Java Platform located under the Libraries node of the project's properties.
Original answer
The path to the JDK used by NetBeans can be found in the netbeans.conf file. Look for the netbeans_jdkhome entry.
You can also specify the jdkhome at runtime (*NIX example given):
netbeans --jdkhome /usr/bin/jdk1.6.0_22
The netbeans.conf file is found in different places depending on what OS you are using. See the NetBeans.conf FAQ on the NetBeans wiki for help finding the file.
A few additional comments...
...You can specify the -target option in the project properties. In NetBeans 6.9 right-click on the project, and choose Properties. Click on the Compiling node. Add your -target to Additional Compiler Options.
...I have read in a few places that specifying a target is not a guarantee that the code will run on a JRE whose version is lower than the JDK that built it. In other words, the recommendation seems to be that if you want 1.5 binaries, then compile with the 1.5 JDK.