Gradle build - not able to import org.json - java

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'

Related

Gradle dependencies: package does not exist

I know this has been asked multiple times but the questions have multiple answers.
I'm trying to use a Java package that's a dependency of my dependency. Let's say I've built this gradle project called "ee_tools". And that has a dependency called "my_models", which has a package called "com.mycompany.my_models.db". So in the ee_tools project build.gradle, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "my_models", version: "1.0.0"
}
Then in my current project, I've got
dependencies {
// My stuff
implementation group: "com.mycompany", name: "ee_tools", version: "1.0.0"
Shouldn't this mean that the public classes in my_models are accessible through ee_tools to my current project? Gradle was able to find both in my artifactory instance. And the gradle dependencies command shows ee_tools under the compileClasspath, implementation, and testCompileClasspath trees, but not with its children dependencies. It also shows up in the runtimeClasspath and testRuntimeClasspath trees with its children dependencies, including my_models.
I am also able to see that package inside the jar on the left side of IntelliJ, under the "External Libraries" tree, along with some classes.
But when I try to use the my_models package in my current project, IntelliJ can't find it and it fails a gradle build with the error
error: package com.company.my_models.db does not exist
It can't find any package in that dependency. What gives? Am I declaring the dependencies incorrectly? Or is this a gap between the gradle binary in my command line vs IntellJ and gradlew?
If ee_tools depends on my_models, then your gradle file in ee_tools should be like
implementation project(path: ":path:to:my_models", configuration: 'default')
:path:to:my_models is defined in settings.gradle in project root path like this:
include ':path:to:my_models'

Graalvm not found within Java application

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

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 add library to classpath

I am using Gradle for a project. My build.gradle has got JUnit as a dependency, but every time I import the project again it does not recognize the JUnit library and asks me to add it to classpath. This problem does not only apply to JUnit, but also to other dependencies. Did I add JUnit to the wrong place?
PS: The project builds just fine, it's just the IDE (IntelliJ) that's marking everything red.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The dependency declaration is correct. Probably you're opening the project in IntelliJ IDEA the wrong way.
I suggest you to open a Gradle project this way:
Click the File | Open menu
Select the build.gradle file
Here is further information about importing Gradle projects (see also the side note in that page).
download the external jar file from mvn repository and add it to libs folder.
Gradle-project(your project)->libs(inside libs folder paste jar file). if you don't have libs folder just create it.
go to build.gradle
dependencies { compile files('libs/your_jar.jar') }
reload the gradle project.
it's worked for me. i hope it's work for you guys also...

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