Graalvm not found within Java application - java

I have an Apache Tomcat servlet in which I would like to run some other language - for this purpose, I would like to try to get Graalvm functionality working within OpenJDK 17.
I added the following dependency to my build.gradle file:
// https://mvnrepository.com/artifact/org.graalvm.sdk/graal-sdk
implementation group: 'org.graalvm.sdk', name: 'graal-sdk', version: '22.1.0'
The Gradle file parses and runs without issue, however when I try to access the Graalvm classes in my application (servlet), nothing is found!?
import org.graalvm; // is not found
For a first test, I'd like to get something simple like the following to work:
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
...
try (Context context = Context.create()) {
context.eval("js", "print('Hello JavaScript!');");
}
But this fails, as the required class (Context) is not found. I'm sure I'm missing something simple, but what?
Update
Thanks to #Mohammed Fataka suggestion, the Gradle build now runs successfully. My IDE (eclipse) was still showing compile errors, however a "Refresh Gradle Project" resolved that issue. (Right-click on the project within the Package Explorer, then Gradle -> Refresh Gradle Project).
I now have a new problem in that the resultant servlet crashes with the following error message: IllegalStateException: No language and polyglot implementation was found on the classpath. Make sure the truffle-api.jar is on the classpath.
So I added the following dependency:
// https://mavenlibs.com/maven/dependency/com.oracle/truffle-api
implementation group: 'com.oracle', name: 'truffle-api', version: '0.8'
But this hasn't helped, the same error still occurs.
Update 2
I finally have it working, I needed to use a different dependency:
// https://mvnrepository.com/artifact/org.graalvm.truffle/truffle-api
implementation group: 'org.graalvm.truffle', name: 'truffle-api', version: '22.1.0'
// https://mvnrepository.com/artifact/org.graalvm.js/js
implementation group: 'org.graalvm.js', name: 'js', version: '22.1.0'
I hope this helps others...

hey there please try to see if you are required to add compiler for that, if yes then add to your plugins section in build.gradle
id "org.graalvm.plugin.compiler" version "0.1.0-alpha2"
btw,i run the code you got i have no issue and i can access to them, try to see your repositories as well.
docs are here

Related

InvalidModuleDescriptorException while importing dl4j into my project

I’m trying to use dl4j in my project which runs on openjdk-15.0.2.7 and I use gradle to manage my dependencies. The project builds with no errors but when the code is executed it crashes immediately with the error:
java.lang.module.InvalidModuleDescriptorException: Provider class com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReaderSpi not in module
I haven’t really worked with java modules, since I have only used java 8 so far but to me this looks like its missing a bunch of packages and I don’t know what to do about it. Do I need to put something into the module-info.java file? I have not used any dl4j classes or methods so far in the project.
Thanks in advance.
Here is how I added the dependencies:
implementation group: ‘org.deeplearning4j’, name: ‘deeplearning4j-core’, version: ‘1.0.0-beta6’
implementation group: ‘org.nd4j’, name: ‘nd4j-cuda-10.2-platform’, version: ‘1.0.0-beta6’

Dependency version not resolved from ext block build.gradle

I have added some dependency in my build.gradle file like this
ext {
boxableVersion = '1.5.bq'
}
dependencies {
implementation group: "com.github.dhorions", name:'boxable', version:${boxableVersion}
}
This was working seamlessly until I changed my JDK version from 1.8 to 11. Now, When I am trying to build the project, the following error shows up in my build.gradle file
Could not run phased build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-6.0-bin.zip'.
Build file '/home/christine/christine/projectsFromGit/pdfcreator/build.gradle' line: 43
A problem occurred evaluating root project 'pdfcreator'.
Could not find method $() for arguments [build_4fl1snfk49qgbmumnf1gg989h$_run_closure3$_closure11#d7c9f2a] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
If i give version directly like this, the project is building successfully
implementation group: "com.github.dhorions", name:'boxable', version:'1.5.bq'
How can i give versions externally like I was doing before?
The following syntax version:${boxableVersion} in your dependency declaration is not valid. It's not related to your JDK version, it's just that ${...} means nothing in Gradle DSL or Groovy syntax.
If you want to define version in an 'ext' property, you can then reference this property directly with version: boxableVersion (ext properties are made available directly in the script through the "groovy magic"), or you can use Groovy String interpolation notation (please note the double-quote ")
version: "${boxableVersion}"
Or , in a simpler way:
implementation "com.github.dhorions:boxable:${boxableVersion}"
Just try:
ext {
boxableVersion = '1.5.bq'
}
dependencies {
implementation group: "com.github.dhorions", name:'boxable', version:"${boxableVersion}"
}

New Gradle dependency not added to project and external dependencies (VSCode)

I've been having this issue on and off for the past few weeks after beginning to learn how to use Gradle.
I added the dependency to my build.gradle file
compile group: 'org.apache.xmlgraphics', name: 'batik-all', version: '1.12', ext: 'pom'
I used VSCode's command palette to "refresh", cleaned the server workspace and ran gradle build but the new dependency does not show up in my "Project and External Dependencies" and I cannot import org.apache.batik.*
Hoping to find a fix for this in VSCode as I have seen some other IDE specific fixes.
So the issue was that in the map notation you were specifying the ext: 'pom' part. This effectively told Gradle to only import the POM file and not treat the dependency as a regular one.
When you moved to the different notation, you did not keep that pom element in the coordinates and so you got the right behaviour.
Note: compile has been replaced by implementation for a while now and is even deprecated in Gradle 6.x. Have a look at the documentation for more information on this.

Gradle build - not able to import org.json

I have a problem where I’m trying to include org.json as a dependency in my gradle/spring boot project. In my build.gradle I have
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.json:json:20141113")
}
But in the editor window it looks like IntelliJ is not able to import org.json - it’s unable to find the package
What’s weird is that I can run the gradle ‘build’ task without errors in IntelliJ, and I can run
./gradlew clean build && java -jar build/libs/MyApp-0.1.0.jar
and it works fine.
Is this a ‘fat jar’ problem? I’m not sure it is because org.json is definitely not in my class path on the command line. Any advice appreciated.
I was able to successfully download json in my gradle dependencies:
implementation 'org.json:json:20171018'
As some mentioned, mvnrepository provides snippet for different Build systems.
After you choose your desired version, in the middle of the page, select Gradle as build system, copy and paste the snippet in your build.gradle file, section dependencies in your java project.
// https://mvnrepository.com/artifact/org.json/json
compile group: 'org.json', name: 'json', version: '20180813'

Locally downloaded library not found by Gradle when compiled

I'm not used to post questions so please excuse my mistakes.
I'm looking into moving to Gradle+Nexus for my company and I've hit a wall.
I will try to describe my problem as clearly as possible.
I wrote 2 simple java programms, one depends on the other, both depends on junit to run theirs tests.
I could compile, test and run them while I was using Maven repository but one of the goals of using Nexus is to have 3rd party libraries on it as well as ours. While using Maven I could upload my builds to Nexus and also use them to compile with (through the dependencies) so no problem there.
But when I downloaded junit and uploaded it to Nexus I could not compile anymore.
Here is the error message I got:
Could not resolve all dependencies for configuration ':Project2:testCompile'.
>Could not find junit.jar (junit:junit:4.12).
Searched in the following locations:
http://mycompany/nexus/content/repositories/TRUNK_3rdParty/junit/junit/4.12/junit-4.12.jar
So gradle looks at the right place but cannot find junit.
I should mention that I uploaded junit using the POM file provided by Maven so everything should work.
As it might be of help, here are some code fragments from my build.gradle file:
repositories {
maven {
url "http://mycompany/nexus/content/repositories/TRUNK/"
}
}
dependencies {
compile group: 'mycompany', name: 'project1Nexus', version: '1.0-TEST'
}
that tells project2 where to find project1 on Nexus in order to compile (it works).
repositories {
maven {
url "http://mycompany/nexus/content/repositories/TRUNK_3rdParty/"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
and that should tell where to find junit but I get this error message.
Also the first repo/depend block is from the build.gradle file of project2 and the second one is from the one of the root folder. I doubt this could be an issue though as it worked fine when I used centralMaven.
If anyone had some insights to share, it would be amazing!
Cheers

Categories