How do i add the jar files to intellij properly. The program works in intellij but not as a .jar file.
I have tried adding the files and exporting them. I dont know what to do. I've been going around in circles for the last 6 hours. Its obiously something to do with the runtime but i dont know anything about that and either there is not much information on it or (most probably) im not googling the right things.
Its this error Caused by: java.lang.NoClassDefFoundError: org/apache/poi/xwpf/usermodel/XWPFDocument
at Methods.word_output.createDocx(word_output.java:28)
at Controllers.mainController.createReport(mainController.java:466)
... 53 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xwpf.usermodel.XWPFDocument
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 55 more
A good option is to use a Gradle build.gradle file in the project main directory and then import the project in IntelliJ via "Import project" and choosing that file. This way IntelliJ resolves all the necessary dependencies for you.
A sample minimal Gralde build-file is
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.poi:poi:3.15-beta1'
compile 'org.apache.poi:poi-ooxml:3.15-beta1'
testCompile "junit:junit:[4.12,)"
}
Ideally you would follow the layout of Gradle builds and put your sources in src/main/java and your tests in src/test/java.
As a bonus you gain the possibility to build the project on the commandline/CI/... whatever!
Related
I am using log4j in android jar module. I can build the jar file successfully and run it in AndroidStudio successfully.
My gradle config is:
implementation 'log4j:log4j:1.2.17'
But when I try the jar file from command line:
java -jar test.jar
I got below error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.yeetor.Main.<clinit>(Main.java:39)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Why it can run in AndroidStudio but not work from command line?
Refer to this SO, to generate one jar file include dependency. the dependency should be included using "compile" but not "implementation", then you will get a bigger Jar file include all dependency. Usually gradle will not include dependency in Jar file but generate all each dependency as a single jar file.
My company admin helped me to install the Android Studio, but when I try to create a project, it come with error
Error:A problem occurred configuring project ':app'.
> Could not download junit.jar (junit:junit:4.12)
> Could not get resource 'https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.jar'.
> Could not GET 'https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.jar'.
> sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
After investigate the log file, I found that Gradle cannot download the JUnit library, which possibly caused by firewall.
org.gradle.internal.resource.transport.http.HttpRequestException: Could not GET 'https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.jar'
However, if I use browser go to this URL, I'm able to grab this jar (with warning, but can bypass it).
So my question is, can I pass this jar to Gradle or append to classpath for Gradle?
And one more thing, I don't have admin right and can't modify the Program Files
Thanks in advance!
UPDATE
At the end, I have solved this problem by following steps:
Fix the SSL problem, as my company's firewall will alter the certificate, I need to add that into java keystore (Details in Here)
After fixed the SSL problem, it throw another exception Failed to resolve: junit:junit:4.12 and more other library, I follow another post "Error:(23, 17) Failed to resolve: junit:junit:4.12" to solve this.
Hope this can help if anyone have the same problem.
Happy coding~~~
A very simple way is you can put the junit jar file in the android project "libs" folder
Gradle will add this to the classpath, if you open the build.gradle file, you will get something like
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
......
}
When I build the Amazon (Kindle) flavor of my Android app I run into this Runtime error:
Caused by: java.lang.RuntimeException: Stub!
at com.amazon.device.messaging.ADMMessageReceiver.<init>()
I need the local amazon-device-messaging.jar file to compile my app, however I do not need to include it during runtime as the amazon device will have the necessary classes and methods.
How do I update my Android Studio build.gradle file to do this?
I also ran into this issue. When adding the Amazon Device Messaging jar as a library, Android Studio automatically generated
dependencies {
compile files('libs/amazon-device-messaging-1.0.1.jar')
}
I just needed to switch that to
dependencies {
provided files('libs/amazon-device-messaging-1.0.1.jar')
}
That did the trick for me. I'd up-vote your answer, #Clu, but I don't have a high enough reputation.
To solve this I used the provided type of dependency.
Inside my project modules build.gradle file, right before my dependencies closure I included the following:
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
And then, within my dependencies closure I included the following:
dependencies {
provided files('libs/amazon-device-messaging-1.0.1.jar')
}
This ensured that the .jar was only used for compile time and not runtime. I'm quite new to Android Studio, and this took me a while to figure out; hopefully this will help you make the switch to Android Studio as well.
Add the ADM jar in the Maven local repository.
Command :
mvn install:install-file "-Dfile=amazon-device-messaging-1.0.1.jar" "-DgroupId=com.amazon.device.messaging" "-DartifactId=amazondevicemessaging" "-Dversion=1.0.1" "-Dpackaging=jar"
Include local maven repository as project dependency :
Add “mavenLocal()” in main Gradle build script:
allprojects {
repositories {
mavenCentral()
mavenLocal()
}
Link the Maven artifact in ADM project.
Add below line ADMWrapperLib Gradle script (::).
provided 'com.amazon.device.messaging:amazondevicemessaging:1.0.1'
I downloaded the latest Android Studio (Mac), I got this error when I try to create a new project:
Failed to import Gradle project: could not fetch model of type IdeaProject using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'
Would anybody know how I can move past this problem?
Try changing in the build.gradle file:
:MyApplication1 -> :MyApplication1Project:MyApplication1
for all references
I can't say for sure without seeing your build.gradle file, but at line 9, do you have the line:
apply plugin: 'AppPlugin'
If this is the case the problem is that you're attempting to build an android application but you're applying the wrong plugin. If this is an Application, it should be:
apply plugin: 'android'
And if it's a library project then it should be:
apply plugin: 'android-library'
Again though, I can't say for sure if this is the problem because I haven't seen your build.gradle file.
how to export a executable jar in gradle, and this jar can run as it include reference libraries.
build.gradle
apply plugin: 'java'
manifest.mainAttributes("Main-Class" : "com.botwave.analysis.LogAnalyzer")
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
after i run : gradle build
it create the build folder, and i run the jar in build/libs/XXX.jar:
java -jar build/libs/XXX.jar
here is a execution says :
Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
how can i run it with the reference libraries?
You can achieve it with Gradle application plugin
Hopefully this helps someone (as I unfortunately spent quite some time trying to find the solution). Here's the solution that worked for me for creating an executable JAR. I'm embedding Jetty in the main method, Jetty 9 to be specific and using Gradle 2.1.
Include the following code into your build.gradle file (if a subproject is the "main" project that the jar needs to be built from, then add it to the subproject which should start like this project(':') { insert the code somewhere here, after dependencies.}.
Also, you need to add the plugin java for this to work: apply plugin: 'java' .
My jar task looks as follows:
apply plugin: 'java'
jar {
archiveName = "yourjar.jar"
from {
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'your.package.name.Mainclassname'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
And then you can execute your yourjar.jar via the commandline:
java -jar yourjar.jar
The META-INF/.RSA, META-INF/.SF and META-INF/*.DSA have to be excluded for it to work. Otherwise a SecurityException gets thrown.
The problem seems to lie with embedded Jetty, as Jetty moved to Eclipse and now is signing their JARs, which I read becomes problematic when other, unsigned JARs want to load the signed ones. Please feel free to educate me if I am wrong in this, that's just what I read.
The JARs that the project depends on are defined in the dependencies as follows:
dependencies {
// add the subprojects / modules that this depends on
compile project(':subproject-1')
compile project(':subproject-2')
compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.2.6.v20141205'
compile group: 'org.eclipse.jetty', name: 'jetty-servlet', version: '9.2.6.v20141205'
compile group: 'org.eclipse.jetty', name: 'jetty-http', version: '9.2.6.v20141205'
}
EDIT: Before, instead of just
configurations.runtime.collect{...}
i had
configurations.runtime.asFileTree.files.collect{...}
This caused strange behaviour in a larger project in clean build. When running the jar after executing gradle clean build for the first time (after manually cleaning the build directory), it would throw a NoClassDefFoundException (in our project with many subprojects), but running the jar after executing gradle clean build a second time (without emptying the build directory manually), for some reason it had all dependencies. This didn't happen if asFileTree.files was left out.
Also I should note, all compile dependencies are included in runtime, however not all runtime are included in compile. So if you are just using compile
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
Then be sure to remember that if there is a NoClassDefFoundException thrown, some class isn't found at runtime, which means you should also include this:
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
Quick answer
Add the following to your build.gradle:
apply plugin: 'application'
mainClassName = 'org.example.app.MainClass'
jar {
manifest {
attributes 'Main-Class': mainClassName,
'Class-Path': configurations.runtime.files.collect {"$it.name"}.join(' ')
}
}
From the project directory, run gradle installDist
Run java -jar build/install/<appname>/lib/<appname>.jar
I recommend adding the app version to your build.gradle as well, but it's not required. If you do, the built jar name will be <appname>-<version>.jar.
Note: I'm using gradle 2.5
Details
In order to create a self contained executable jar that you can simply run with:
java -jar appname.jar
you will need:
your jar to include a MANIFEST file pointing to your application main class
all your dependencies (classes from jars outside of your application) to be included or accessible somehow
your MANIFEST file to include the correct classpath
As some other answers point out, you can use some third-party plugin to achieve this, such as shadow or one-jar.
I tried shadow, but didn't like the fact that all my dependencies and their resources were dumped flat out into the built jar together with my application code. I also prefer to minimize the use of external plugins.
Another option would be to use the gradle application plugin as #erdi answered above. Running gradle build will build a jar for you and nicely bundle it with all your dependencies in a zip/tar file. You can also just run gradle installDist to skip zipping.
However, as #jeremyjjbrown wrote in a comment there, the plugin does not create an executable jar per se. It creates a jar and a script which constructs the classpath and executes a command to run the main class of your app. You will not be able to run java -jar appname.jar.
To get the best of both worlds, follow the steps above which create your jar together with all your dependencies as separate jars and add the correct values to your MANIEST.
All of these answers are either wrong or out of date.
The OP is asking for what is known as a "fat jar". That is an exectuable jar which contains all the dependencies so that it requires no outside dependencies in order to run (except for a JRE of course!).
The answer at the time of writing is the Gradle Shadow Jar plugin, explained pretty clearly at Shadow Plugin User Guide & Examples.
I struggled a bit. But this works:
put all these lines somewhere in your build.gradle file (I put them near the top) :
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
shadowJar {
baseName = 'shadow'
classifier = null
version = null
}
jar {
manifest {
attributes 'Class-Path': '/libs/a.jar'
attributes 'Main-Class': 'core.MyClassContainingMainMethod'
}
}
PS don't worry about any other "repositories", "dependency" or "plugin" lines elsewhere in your build file, and do leave the lines thus inside this "buildscript" block (I haven't a clue why you need to do that).
PPS the Shadow Plugin User Guide & Examples is well-written but doesn't tell you
to include the line
attributes 'Main-Class': 'core.MyClassContainingMainMethod'
where I've put it above. Perhaps because the author assumes you are less clueless than I am, and you probably are. I haven't a clue why we are told to put a strange "Class-Path" attribute like that in, but if it ain't broke don't fix it.
When you then go
> gradle shadowjar
Gradle will hopefully build a fat executable jar under /build/libs (default name "shadow.jar") which you can run by doing this:
> java -jar shadow.jar
I checked quite some links for the solution, finally did the below mentioned steps to get it working. I am using Gradle 2.9.
Make the following changes in your build,gradle file :
1. Mention plugin:
apply plugin: 'eu.appsatori.fatjar'
2. Provide the Buildscript:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "eu.appsatori:gradle-fatjar-plugin:0.3"
}
}
3. Provide the Main Class:
fatJar {
classifier 'fat'
manifest {
attributes 'Main-Class': 'my.project.core.MyMainClass'
}
exclude 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.SF'
}
4. Create the fatjar:
./gradlew clean fatjar
5. Run the fatjar from /build/libs/ :
java -jar MyFatJar.jar