I am trying to build an jar artefact from this repository. I imported apache commons io and org.json as libraries. When extracting the artifact I find a Manifest file which only contains the information of org.json. You can find the jar here. The manifest file in my Project is not reflected at all. Any help is appreciated. When I run the jar in console with java -jar I get the
Error: No Main Manifest Attribute in XXX.jar.
From the project you provided on github i made the following changes to make it run with java -jar. But first and foremost the project you linked on github does not build with gradle build on a fresh pull.
to make this run you need to add commons io and org.json to you build.gradle file. this makes the build.gradle file look like:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-io', name: 'commons-io', version: '2.6'
compile group: 'org.json', name: 'json', version: '20190722'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
manifest {
attributes(
'Main-Class': 'de.bergwacht.esslingen.Main'
)
}
}
your project also has alot of unused dependencies that should be removed that were creating gradle warnings.
in AnweseneheitsTableModel remove the imports:
import com.sun.org.apache.xpath.internal.operations.Bool;
in DienstprotokollInvalidArgumentException remove the imports:
import com.sun.javaws.exceptions.InvalidArgumentException;
in MainForm remove the imports:
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.sun.xml.internal.fastinfoset.algorithm.BooleanEncodingAlgorithm;
at this point you can run ./gradlew jar and this will create a jar for you under build/libs
you can run that jar with java -jar build/libs/BWOrgaTool-1.0-SNAPSHOT.jar
that command will run it, it will encounter a NPE:
Exception in thread "main" java.lang.NullPointerException
at de.bergwacht.esslingen.forms.MainForm.<init>(MainForm.java:138)
at de.bergwacht.esslingen.Main.main(Main.java:42)
but thats another problem all together, you can start debugging your program at that point.
Related
I have a multi module project in IntelliJ, a java library project, using Gradle with quite old versions of libraries
I must say, and now switching to Java 11 (using right now OpenJDK 11.0.2 from https://jdk.java.net/archive/).
I want to modularize that library project, adding to all modules a module-info.java.
I keep getting an error in one of the modules with one of the dependencies, Saxon-HE.
I isolated that module in a separate project (using Gradle 7.6), and modified the build.gradle dependencies step by step as IntelliJ discovered
import errors, while using the latest versions of the dependencies.
The build.gradle of the project looks like this:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The build.gradle of the module looks like this up to the point with the error with Saxon-HE.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
The module-info.java looks like this, I used the recommendations of IntelliJ so it added automatically the requires statements:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
}
The next import error IntelliJ discovers while building it results because one of my classes has the import statement:
import net.sf.saxon.xpath.XPathFactoryImpl;
Building the project results in the error:
error: package net.sf.saxon.xpath does not exist
import net.sf.saxon.xpath.XPathFactoryImpl;
Looking at https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/11.4 I added to build.gradle of the module:
implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '11.4'
In IntelliJ I can see in the Project navigator view, in External Libraries that there is
Gradle: net.sf.saxon.Saxon-HE:11.4
Saxon-HE-11.4.jar
IntelliJ recommends
Add 'requires Saxon.HE' directive to module-info.java
The module-info.java looks now like this:
module mymodule1 {
requires org.apache.commons.lang3;
requires org.apache.commons.io;
requires org.slf4j;
requires okhttp3;
requires okio;
requires java.xml;
requires org.jdom2;
requires Saxon.HE;
}
After that the error in the particular class using that import statement is gone, IntelliJ doesn't complain.
But when then building the project I get the error
C:\Users\ME\PROJECTS\myproject\mymodule1\src\main\java\module-info.java:9: error: module not found: Saxon.HE
requires Saxon.HE;
^
Removing the requires Saxon.HE and building the project results in the error:
error: package net.sf.saxon.xpath is not visible
import net.sf.saxon.xpath.XPathFactoryImpl;
^
(package net.sf.saxon.xpath is declared in the unnamed module, but module net.sf.saxon.xpath does not read it)
I find this error message weired, because it says but module net.sf.saxon.xpath does not read it, I would rather expect but module mymodule1 does not read it.
I don't know what's going wrong, other external dependencies are not problematic but Saxon-HE is.
I found here Gradle build - add module path a snippet which might solved it, but maybe only partially, so not sure if this answer can be marked as the solution.
I added to the project build.gradle
subprojects {
apply plugin: 'java'
sourceCompatibility = 19
targetCompatibility = 19
compileJava {
doFirst {
options.compilerArgs += [
'--module-path', classpath.asPath,
'-Xmaxerrs', 1000
]
classpath = files()
}
}
}
Now trying to build it (with Gradle 7.6 and OpenJDK 19), it doesn't complain to not finding modules, so far at least, but now I have the next problem which I desribed here Java Modules, Gradle, external dependencies - Modules reading from more then one
I need to use Jetty and Vaadin and build a fat jar.
My workspace is based around Gradle 5, Its a gradle multi module project
Parent
Common-Lib
Core-Lib
Jetty+Vaadin
I followed the documentation which i found here:
https://vaadin.com/tutorials/embedded-jetty-server-in-vaadin-flow
The documentation explains how to create Jetty's WebAppContext and start Jetty Server instance all based around maven.
Expect as i said my workspace is based around gradle, so instead of copying the pom.xml i rewrote it to the gradle build script which looks as follows:
plugins {
id 'idea'
}
group = 'some.example.jetty.and.vaadin.fatjar'
version = '1.0.0'
dependencies {
implementation project(':Core-Lib')
implementation project(':Common-Lib')
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compile group: 'org.eclipse.jetty', name: 'jetty-continuation', version: '9.4.14.v20181114'
compile group: 'org.eclipse.jetty.websocket', name: 'websocket-server', version: '9.4.14.v20181114'
compile group: 'org.eclipse.jetty.websocket', name: 'javax-websocket-server-impl', version: '9.4.14.v20181114'
compile group: 'com.vaadin', name: 'vaadin-core', version: '12.0.7'
}
My problem is that during the build gradle outputs a .war file. I cannot use a .war file.
This project is supposed to be a plug-in module for another application, which i do not have sources for. The application just loads a jar files from specific folder, only jar extension is supported.
My question is: How can i create standard "unshaded" uber jar/fat jar instead of .war
With the word "unshaded" i want to unpack all JAR dependencies, and repack them into the final JAR.
I am using Gradle to build my Java project in Eclipse. gradle.build is as follows
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0'
compile group: 'org.reflections', name: 'reflections', version: '0.9.10'
}
All libraries are functioning properly when run through Eclipse. But sometimes it is useful to work on the command line. When run on the command line, the runtime error Exception in thread "main" java.lang.NoClassDefFoundError: com/flowpowered/nbt/regionfile/SimpleRegionFileReader occurs, even though the build is successful and the code contains imports from those libraries. I have tried cleans and rebuilds, along with gradlew build --refresh-dependencies, but I still encounter the same runtime error.
I would assume that the libraries are just never actually imported? Or that they are not being stored where the java project thinks they are? I'm unfamiliar with Gradle, so any advice on this is welcome.
Based on the posted build.gradle file you are not packaging the application as an executable JAR.
First apply the application plugin. But this will not be enough as you won't be able to run the executable as a single JAR without all of its dependencies. Apply the shadow plugin too.
These two plugins will give you access to the following tasks:
run: execute the application from gradle's command line.
runShadow: execute the application but with all dependencies packaged in a single JAR, alongside your compiled classes and resources.
shadowJar: create a single "fat JAR" with compiled classes and all dependencies.
Thus your build.gradle may look like this
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '1.2.4'
}
mainClassName = 'com.acme.YourMainClassName'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0'
compile group: 'org.reflections', name: 'reflections', version: '0.9.10'
}
Plugin documentation:
https://github.com/johnrengelman/shadow
https://docs.gradle.org/3.4/userguide/application_plugin.html#useApplicationPlugin
Another solution without using any plugins and still end up with a runnable fat jar
jar {
archiveName = 'NameOfYourApp.jar'
manifest {
attributes 'Main-Class': 'uk.co.cdl.Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': project.version
}
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
include/exclude anything if need to if not take the curlys off
}
}
I'm trying to run ./gradlew build for a nested multi project structure and I'm running into issues that only seem to appear for projects with test source roots. Being new to both java and gradle I'm sure I'm breaking more than one convention, but I still think this should be working.
Essentially, all the dependencies seem to be added fine, but when I have one project that only has a Test srcDir that depends on another project that has a Test srcDir, it doesn't recognize packages/symbols in that root project. However, projects with regular srcDirs (not test) don't seem to have a problem.
My project has more going on than this, but here is the simplest setup I've tried that illustrates the problem.
My project structure:
qatests
/Applications
/AppGroupName
/AppBasePageObjects
/src
/AppBaseTests
/src
/BasePageObjects
/src
/BaseTests
/src
My settings.gradle in qatests:
rootProject.name = 'QaTests'
include 'BasePageObjects'
include 'BaseTests'
include 'Applications:AppGroupName'
include 'Applications:AppGroupName:AppBasePageObjects'
include 'Applications:AppGroupName:AppBaseTests'
My build.gradle in qatests:
version '1.0-SNAPSHOT'
allprojects {
repositories {
mavenCentral()
}
}
subprojects{
if(project.name.contains("Tests")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
}
}
sourceSets {
test {
java {
srcDir 'src'
}
}
}
}
if(project.name.contains("PageObjects")){
apply plugin: 'java'
sourceCompatibility = 1.8
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.+'
}
if(project.name !="BasePageObjects")
{
dependencies {
compile project(':BasePageObjects')
}
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
}
}
I won't include my build.grade for the BaseTest project since that seems to compile fine during the gradlew build, but here is my build.gradle for AppBaseTests:
version '1.0-SNAPSHOT'
dependencies {
compile 'org.apache.commons:commons-lang3:3.4'
compile project(':Applications:AppGroupName:AppBasePageObjects')
}
When I run ./gradlew build in the qatests root, the BaseTests, BasePageObjects, and AppBasePageObjects projects seem to compile fine and AppBasePageObjects successfully uses packages and symbols from BasePageObjects. For some reason however, AppBaseTests can't seem to recognize packages and symbols in BaseTests.
If I clone this project from scratch, IntelliJ runs the gradle scripts scripts automatically and everything seems to work out of the box just fine with the dependencies, so this just confuses me even more.
I've tried adding compile and testcompile for all the project dependencies since that's the only real difference between AppBasePageObjects which works and AppBaseTests which doesn't work. I've also tried adding compileJava.dependsOn(':BaseTests:build') to the AppBaseTests build.gradle file. And a few other rabbit holes, but nothing seems to have any effect on this dependency issue.
For what it's worth, here is the first error I see in the actual build:
error: package Tests does not exist import Tests.BaseTest;
Any help or suggestions are greatly appreciated. If you would like to distribute some harsh insults I'll take those as well. Thank you.
I believe I found an answer while reading another solution here:
Multi-project test dependencies with gradle
It seems I needed to use testCompile project(':BaseTests').sourceSets.test.output for command line builds as well as testCompile project(':BaseTests') for IDE functionality in my root build.gradle file. This only seems to be required for the test projects.
Here are the actual changes in my root build.gradle file:
if(project.name != "BaseTests")
{
println "$project.name depends on BaseTests"
dependencies {
testCompile project(':BaseTests')
testCompile project(':BaseTests').sourceSets.test.output
}
}
This seems a bit hacky but works, if somebody has a more thorough and intelligent answer please post!
I have followed this guide for creating a custom task with Gradle.
http://www.ysofters.com/2015/02/26/how-to-create-gradle-project-with-custom-task-classes-in-groovy/
I also had a look at the gradle doc. https://docs.gradle.org/current/userguide/custom_tasks.html
It's very clear, and I can get the examples to compile and use the tasks, so everythings fine so far.
However, examples only show the import of gradle api-files, i.e.
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
I want to add some more dependencies i.e
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean
import org.springframework.core.io.FileSystemResource
but I can do that - gradle will complain and say "unable to resolve class .."
I tried to also set up spring-core and apache-commons under the buildscript->dependencies closure in build.gradle but it didn't make any difference i.e.
buildscript {
dependencies {
classpath group: 'org.springframework', name: 'spring-core', version: springVersion
classpath group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
.....
So, what am I missing - how can I use third party dependencies in my custom task class?
you need to put a build.gradle file into your buildSrc directory. You can checkout the buildSrc/build.gradle file from the gradle project itself as an example: https://github.com/gradle/gradle/blob/v4.1.0/buildSrc/build.gradle