Gradle - Class not loaded : org.docx4j.jaxb.Context - java

I had a maven project with old docx4j dependencies, and I wanted to update to java 9. During the update I changed my project from maven to gradle.
So now I have a gradle project in IntelliJ with this dependencies:
dependencies {
implementation('commons-io:commons-io:2.6')
implementation('javax.xml.bind:jaxb-api:2.4.0-b180725.047')
implementation('org.docx4j:docx4j:6.0.1')
implementation('org.docx4j:docx4j-ImportXHTML:6.0.1')
implementation('org.docx4j:docx4j-export-fo:6.0.1')
testImplementation('junit:junit:4.12')
}
The build is working, but if I want to open a .docx file, with Docx4J.load(...) or WordprocessingMLPackage.load(...) it throws a RuntimeException.
In debug mode I can see this message: Class not loaded : org.docx4j.jaxb.Context
This Context.java file has a static code block like this:
static {
...
}
I think it's gradle specific error, because I created a new maven project with the same code and dependencies as the gradle project, and it works.
Is there any solution for this, or should I use maven in the future too?

There is a Gradle-specific answer at https://stackoverflow.com/a/51235096/1031689
Or to "add module" instead, see for example:
https://www.concretepage.com/questions/531
https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin/issues/11
Or mac java 9 gradle ClassNotFoundException: javax.xml.bind.JAXBElement when building

Related

Added Gradle to Java project "Exception ... java.lang.NoClassDefFoundError"

I had an existing project without Gradle and needed to add com.google.code.gson:gson:+ library to work with JSON objects. To begin with I ran either gradle init or gradle build, I'm not sure. This caused my java classes with a main() not to run as the source path was wrong/changed. I have changed the structure following advice to at least get the classes to compile and run, but I still have this warning in run configurations "Warning: Class 'Main' not found in module 'src'" ;
If I set Use classpath of module to src.main, the warning goes away but when I run Main.main() Gradle seems to execute Gradle tasks, like this - this will run indefinitely;
Here is my project structure;
This is my build.gradle file;
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
// idea plugin? // I added this to original build.gradle file
id 'idea'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
google()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// For use with JSONUtil class // I added this to original build.gradle file
compile 'com.google.code.gson:gson:+'
}
application {
// Define the main class for the application.
mainClassName = 'java.Main' // changed to 'Main' and I can `gradle run` seems to actually run Main.java
}
I have imported com.google.gson.JsonObject and com.google.gson.JsonParser from com.google.gson:gson:2.8.6 library, with no code inspection warnings, i.e available at compile time. If I run my code with a JsonObject jsonObject = new JsonObject I get the error;
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/JsonParser
at HttpUtils.getAccessToken(HttpUtils.java:80)
at Main.auth(Main.java:75)
at Main.play(Main.java:36)
at Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonParser
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 4 more
Line 80 of HttpUtils.java;
JsonObject jsonResponse = JsonParser.parseString(response.body()).getAsJsonObject(); // todo: status 200 "success" else failed
accessToken = jsonResponse.get("access_token").getAsString();
System.out.println(accessToken);
I understand this means that JVM can't compile a .class for JsonParser? I suppose this means the compiler has no knowledge of the library existing, which makes me suspect that Gradle isn't configured properly with the project, as it has downloaded the library, but not added a path to it?
I have tried gradle cleanIdea and then gradle idea. I have rebuilt the the project. I have "Mark directory as source root" on various directories for testing being careful to revert when it failed to change behaviour.
Edit;
I have added a package com.example in the src.main.Java directory and added the java files.
I edited run configuration for Main.java to
Main class: com.example.Main
Use classpath of module: src.main
I also changed the build.gradle file to;
application {
// Define the main class for the application.
mainClassName = 'com.example.Main'
}
Main runs but I am stuck at this point, which seems to run indefinitely;
Also, I am sure I right clicked on build.gradle and selected import, although I can't recreate this as the option isn't available now.
Edit 2;
I have been able to get the classes Main and Test with main() to run by putting them in the test/java/src package, and using unusual run configuration with warnings. Although on closer inspection, it seems to be running code that is previously compiled somewhere, as any changes I make aren't reflected in output.
Here is my project structure at the moment;
This is my run configuration that actually runs main in the standard output console, rather than a Gradle Task. It's clearly wrong, as Main is not in the com.example package or src.main module. If I set it correctly using module src.test and main class src.Main Gradle runs as screenshot 5.
Edit 3;
I see now that Gradle has took over responsibility to build and run the java files. I didn't know running in the output could be done with another CLI app and I admit it confused me, so please forgive anything above that seems stupid, I'm learning and figuring this out as I go.
I found in InteliJ settings Build, Execution, Deployment > Build Tools > Gradle I can change the Build and run using option between InteliJ IDEA and Gradle. The only issue I'm having with Gradle now I understand what is happening is Gradle doesn't seem to update my .class files when I run my main() with Gradle. Maybe this is for another question though.
mainClassName = 'java.Main' // changed to 'Main' and I can "gradle run" seems to actually run Main.java
This is not correct. Based on screenshot - you have not package named java (also I doubld that this is a valid name for a Java package). Create proper package inside src/main/java directory and specify it in the Main source file and in build.gradle file.
Also make sure you have imported build.gradle file in IDE, see Link a Gradle project to an IntelliJ IDEA project

Eclipse gradle project setup fake error notification

I want to build a REST Service with Java Spring and followed this guide successfully.
After creating a new gradle project i copied the build.gradle file and started everything with gradle buildRun. It is all woring well, except that Eclipse underlines imports, variable declararations, standard
datatypes as unresolveable.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
For example are in this class String, SpringApplication, SpringBootApplication, Application and org.springframework marked as unresolvable. However Exlipse resolves them just fine. I hope someone can
tell me what i have to change, error detection is quite hard right now (:
The platform is:
Eclipse IDE for Java Developers
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200
OS: Linux, v.4.16.13-2-ARCH, x86_64 / gtk 3.22.30, WebKit 2.20.3
I had this issue before with Spring (although it was with with Maven) and I was able to resolve it by updating my Maven project and running a clean build. Check and see if it possible to "refresh" your gradle build/update dependencies:(For me it was Right-click -> Maven -> Update Project), and also try running gradle clean first, then build it to see if leftover artifacts are causing the issue.
If you get a "Could not find or load main class" or an error like that from Spring later, make sure your directory structure (from the project) is src/main/java/hello, with hello being the package containing your class files.

Using Java & Gradle to build a maven project from github

I need help with the following problem:
Right now, I want to use my Java Gradle Project to build this github-project:
https://github.com/PaperMC/Paper but the project but it's a maven project.
I found a plugin for adding github projects as dependencies but it fails here.
An alternative would be to use this jenkins here: https://ci.destroystokyo.com/job/Paper/
However here I'm struggling to make my gradle always use the newest build without having to reasign the chaning version numbers by hand.
Can someone help me out because I have no idea what and how to add to the build.gradle.
Thanks in advance!
Edit:
I found the Maven-Syntax for the correct pull here:
https://paper.readthedocs.io/en/paper-1.11/developers/#plugin-developers
But when I try to convert the information to Gradle I still fail when I try to leave out the version so that Gradle would fetch the latest one. Can someone provide a valid snipped for this? I have never done this before.
According to this guy, you can use a fake ivy repository.
As https://ci.destroystokyo.com/job/Paper/lastSuccessfulBuild/artifact/paperclip.jar points on the latest build, just set up your repository like this :
repositories {
ivy {
url "https://ci.destroystokyo.com/job/Paper/lastSuccessfulBuild/artifact/"
layout 'pattern' , {
ivy '[module]-ivy.xml'
artifact '[module](.[ext])'
}
}
}
And then, use the dependency (the group is not relevant here)
dependencies {
compile 'anything:paperclip'
}
It will get the right file
$ gradle build
Download https://ci.destroystokyo.com/job/Paper/lastSuccessfulBuild/artifact/paperclip.jar
...

custom library from maven not able to download for different version

I have a custom library uploaded to bintray maven with version 1.0.0
To download it for any project i have used the following code
repositories {
maven {
url "http://dl.bintray.com/<org or user name>/<repo-name>"
}
}
And inside my app level build.gradle i have added the dependency
dependencies {
.....
compile 'my-group-id:artifactId:versionId'
.....
}
Recently i upgraded the versionId to 1.0.2 and it doesn't compile. For version 1.0.0 it compiles fine.
Heres the error i get
Error: Could not find sdk.jar (com.appunfold:sdk:1.0.2).
Searched in the following locations:
http://dl.bintray.com/appunfold/android/com/appunfold/sdk/1.0.2/sdk-1.0.2.jar
I checked the files inside 1.0.2. It contains sdk-1.0.2.aar (not sdk-1.0.2.jar)and compared it with the files inside 1.0.0. It contains the same with the version 1.0.0 i,e sdk-1.0.0.aar.
I couldn't find the reason why it is not able to download for a different version. Anybody faced the same issue?
Found the solution after a deep few hours of research.
I think Android Studio by default searches for jar files. But since in my case only aar files are available i had to specify one more parameter (#aar) while compiling the dependency.
It looks like this
dependencies {
.....
compile 'my-group-id:artifactId:versionId:#aar'
.....
}

Eclipse Gradle buildship import sub project compilation error

I have project xxxWeb using project xxxAPI. Both projects are sub project of a parent project. Project xxxAPI uses a third-party library jar jar1.jar, which has a class somepackage.ClassA. Project xxxAPI itself also has identical somepackage.ClassA that project xxxWeb intends to use.
However, Eclipse load somepackage.ClassA from jar1.jar instead, resulting in compilation error because jar1/somepackage/ClassA doesn't have the necessary fields like in xxxAPI/somepackage/ClassA.
In xxxWeb project classpath, Eclipse place xxxAPI project to the very end, which probably why the compiler pick jar1/somepackage/ClassA instead of xxxAPI/somepackage/ClassA.
This is not a problem in IntelliJ however.
Is there a cure for this?
The build script work fine, so I this is a question on Gradle's Eclipse plugin, and how to manipulate the generated classpath?
This is a bug of gradle as of version 2.14.1. A workaround is to utilize the hook given by Eclipse Gradle plugin to remove the duplicated classpath entries
eclipse {
classpath {
file {
whenMerged { cp ->
cp.entries = cp.entries.unique{a,b -> a.path <=> b.path}
}
}
}
}

Categories

Resources