I'm working on a project that assembles and builds with Gradle (though I've been using eclipse to write the code), and have been having trouble using Apache's PDFBox. I have set the classpath to the PDFBox .jar, typing echo %CLASSPATH% in cmd returns:
C:\Users\MY_NAME\.m2\repository\org\apache\pdfbox\pdfbox\1.8.6\pdfbox-1.8.6.jar
I was doing research on this earlier and someone said that you need to start the classpath with .;, which had no effect on any of my attempts to fix my problems. Anyways, when I try to compile the program with the gradlew.bat wrapper, I get these errors:
error: package org.apache.pdfbox.pdmodel does not exist
error: package org.apache.pdfbox.util does not exist
the lines these errors reference are at the very top of my .java file, simply:
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;
I tried multiple different ways of editing the build.gradle file based on both the gradle documentation and other examples of build.gradle files I have seen online. The base build.gradle file is as follows:
allprojects {
repositories {
mavenCentral()
maven {
url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
I have tried adding a dependencies{} section in multiple places. The build.gradle file looking like this:
allprojects {
repositories {
mavenCentral()
maven {
url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
}
}
}
dependencies {
compile 'org.apache.pdfbox:pdfbox:1.8.6'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
compiled just fine, but gave me the same include errors described above. Putting the contents of dependencies{} right after repositories{} in allprojects{} gave me the following error:
Could not find method compile() for arguments [org.apache.pdfbox:pdfbox:1.8.6] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated#7051777c.
I have also tried adding apply plugin: 'java' and apply plugin: 'eclipse' at the top of the build.gradle file, but no combination of any of these fixes work. I have copy/pasted the pdfbox-1.8.6.jar file into the project's libs folder, and that doesn't seem to help it either. Is there anyone out there who can help me include the pdfbox-1.8.6.jar file in my gradle build?
The only time I got the error you got was when I removed the "apply plugin" line. Anyway, here's my gradle script that worked:
apply plugin: 'java'
sourceCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty('mainClass')) {
ext.mainClass = 'org.tilman.HelloWorld'
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile 'org.apache.pdfbox:pdfbox:1.8.6'
}
and here's my java program:
package org.tilman;
import org.apache.pdfbox.pdmodel.PDDocument;
public class HelloWorld
{
public static void main(String[] args)
{
PDDocument doc = new PDDocument();
System.out.println("Hello world: " + doc);
}
}
Related
I have a fat jar which is generated by using gradle script. Post the gradle script when I run the following command :-
java -jar fileName.jar
it is running the main method and things are fine. Nevertheless when I try to obfuscate this jar, the resulting jar is complaining that :-
Error: Invalid or corrupt jarfile ObfusactedTest.jar
My code is as follows:-
build.gradle:-
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath files("E:\\softs\\ZKM\\ZKMEval\\ZKM.jar") //ZKM_JAR_PATH must be set to point to your ZKM.jar
classpath 'com.zelix.gradle:plugin:1.0.0'
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'com.zelix.gradle.plugin'
group = 'com.github.jitpack'
sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
}
jar {
manifest {
attributes "Main-Class": "com.github.jitpack.Hello"
}
zip64 = true
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
zkmSetting {
scriptName = "Obfuscate.txt" //Must be set to point to the ZKM Script to execute.
}
Obfuscate.txt:-
print "Obfuscating fatJar.....";
classpath
"C:\Program Files\Java\jdk-10.0.2\lib\jrt-fs.jar"
".\obfuscateFatJar.jar";
open ".\obfuscateFatJar.jar" {"*.class"};
exclude org.apache.commons.*.*;
exclude com.github.jitpack.Hello.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
By the way Hello.java has got the main method.
Your ZKM Script "open" statement specifies the {"*.class"} file filter. So you are filtering out ALL non-class files including your MANIFEST.MF. See https://www.zelix.com/klassmaster/docs/openStatement.html#filter.
A missing MANIFEST.MF will give you a "Invalid or corrupt jarfile" error. Note that your Zelix KlassMaster log file will contain messages like the following.
MESSAGE: Filtering out path 'obfuscateFatJar.jar!META-INF/MANIFEST.MF' because it does not match specified filter '{".class"}>' (D)*
You can work around this by not using a file filter (the safest option in this case) or by broadening your file filter to include other file types. E.g. {".class" || ".MF"}
Update the filter in the class path. The code looks like this now. Works like a charm.
execute "del ObfusactedTest.jar";
classpath
"C:\Program Files\Java\jdk-10.0.2\lib\jrt-fs.jar"
".\obfuscateFatJar.jar";
open ".\obfuscateFatJar.jar" {"*.class" || "*.MF"};
exclude org.apache.commons.*.*;
obfuscate keepInnerClassInfo=false
keepGenericsInfo=true
exceptionObfuscation=heavy
encryptStringLiterals=flowObfuscate;
saveAll archiveCompression=asIs
deleteEmptyDirectories=true
deleteXMLComments=false
"ObfusactedTest.jar";
I am trying to generate code for a simple protobuf example using the build instructions given here. I have been trying for awhile but I am not able to see any auto generated code in my source root.
The following is my build.gradle file
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'
group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.5.1-1"
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
dependencies {
compile 'io.grpc:grpc-netty-shaded:1.14.0'
compile 'io.grpc:grpc-protobuf:1.14.0'
compile 'io.grpc:grpc-stub:1.14.0'
}
Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf
Things I have tried
Sync gradle tool in IntelliJ. This is the most prominent solution
given in SO
Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run
actions on gradle to true
Clean rebuilding of the gradle project.
From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you. What am I missing?
You've applied idea plugin, but you didn't configure it. You need to tell idea plugin where to include the generated source code.
protobuf {
generatedFilesBaseDir = "$projectDir/src/generated"
}
idea {
module {
sourceDirs += file("${projectDir}/src/generated/main/java");
sourceDirs += file("${projectDir}/src/generated/main/grpc");
}
}
You can take a look
at a full example of a buildfile here: build.gradle
In case anyone else ends up here problems getting IntelliJ to recognise the generated sources (Red highlight imports , classes etc). Beware of the intellisense file size limit. If your generated protobuf code exceeds the default setitng of 2500KB then the file is ignored.
Got Help -> Edit custom properties and add an entry appropriate for your case e.g.
idea.max.intellisense.filesize=4000
Spent half a day faffing over different source set source folder, generated sources, and include / exclude directories. Turned out I just need to increase this value
Alternatively, you can use sourceSets:
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
I'm new to Kotlin and Gradle, and tried to follow these steps, so I got the following 2 files:
after running gradle init I changed the build.gradle to be:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
apply plugin: 'application'
mainClassName = "hello.main"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Hello.kt:
package hello
fun main(args: Array<String>) {
println("Hello World!")
}
Then I run the gradle build and got the build\classes\main\hello\HelloKt.class
my question is: Why the file generated is .class not .jar and how to get the .jar file and how to run it, I tried running the generated file using kotlin -classpath HelloKt.class main but got an error error: could not find or load main class hello.main
The classes are the direct output of the Kotlin compiler, and they should be packaged into a JAR by Gradle afterwards. To build a JAR, you can run the jar task, just as you would in a Java project:
gradle jar
This task is usually run during gradle build as well, due to the task dependencies.
This will pack the Kotlin classes into a JAR archive (together with other JVM classes, if you have a multi-language project), normally located at build/libs/yourProjectName.jar.
As to running the JAR, see this Q&A for a detailed explanation: (link)
Thanks for #hotkey answer, it helped me going the correct way.
First of all there is a mistake in the main class declaration, as it should follow the new methodology, that is in the below format:
mainClassName = '[your_namespace].[your_arctifact]Kt'
namespace = package name
arctifact = file name
so, considering the names given in the example above where filename is: Hello.kt, and the namespace is hello, then:
mainClassName = `[hello].[Hello]Kt`
using the previous method, that contains:
apply plugin: 'application'
mainClassName = 'hello.HelloKt'
the generated .jar file is not including the kotlin runtime, so the only way to execute it, is by:
d:/App/build/libs/kotlin -cp App.jar hello.HelloKt
but in order to generate a self contained jar that can be self-executed, and contains the kotlin runtime then the build.gradle should be written as:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.2-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes 'Main-Class': 'hello.HelloKt'
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
followed by gradle build, the [your_working_folder].jar file will be generated at the build/libs folder, assuming the working folder name is app, then file app.jar will be generated.
To run this file, one of the following 2 commands can be used:
D:\App\build\libs\java -jar App.jar
OR
D:\App\build\libs\kotlin App.jar hello.HelloKt
I've followed the instructions to creating a Gradle project using JMonkey but I have been unable to get any of the assets to load as stated in the tutorial:
http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_asset
My build.gradle looks like this:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
maven {
url 'http://updates.jmonkeyengine.org/maven'
}
}
dependencies {
compile 'com.jme3:jme3-core:3.0.+'
compile 'com.jme3:jme3-effects:3.0.+'
compile 'com.jme3:jme3-networking:3.0.+'
compile 'com.jme3:jme3-plugins:3.0.+'
compile 'com.jme3:jme3-jogg:3.0.+'
compile 'com.jme3:jme3-terrain:3.0.+'
compile 'com.jme3:jme3-blender:3.0.+'
compile 'com.jme3:jme3-jbullet:3.0.+'
compile 'com.jme3:jme3-niftygui:3.0.+'
compile 'com.jme3:jme3-desktop:3.0.+'
compile 'com.jme3:jme3-lwjgl:3.0.+'
}
The sample was created from the wiki: http://wiki.jmonkeyengine.org/doku.php/jme3:maven
However the wiki makes no references to assets or how to build them.
After looking through the internet I found that the jar that I'm looking for is the jME3-testdata.jar. According to this conversation: http://hub.jmonkeyengine.org/t/official-maven-repo-for-jme3-0-stable-available-please-test/30571
It was a deliberate decision not to include the test data jar. Because of this, I went ahead and manually downloaded the missing jar and added it to my classpath.
I am working on a java project which uses Gradle to build. I wanted to use the Eclipse WindowBuilder to help with the GUI work, so I used the Gradle Eclipse plugin, generated the Eclipse files, and imported it into Eclipse.
The problem is that none of my imports are resolving. The project builds fine using Gradle, but Eclipse can't import anything for my project. How do I solve this?
I don't know which files to include to help debug this, so if something might help let me know and I will include it.
Thanks in advance for your help!
EDIT: Here is my build.gradle file:
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
buildscript {
def rosMavenPath = "$System.env.ROS_MAVEN_PATH".split(':').collect { 'file://' + it }
def rosMavenRepository = "$System.env.ROS_MAVEN_REPOSITORY"
repositories {
rosMavenPath.each { p ->
maven {
url p
}
}
mavenLocal()
maven {
url rosMavenRepository
}
}
dependencies {
classpath group: 'org.ros.rosjava_bootstrap', name: 'gradle_plugins', version: '[0.1,0.2)'
}
}
apply plugin: 'catkin'
apply plugin: 'eclipse'
allprojects {
/*
A github url provides a good standard unique name for your project
Example below, but you may wish to switch to your own unique url.
*/
group 'com.github.rosjava.gui_test'
version = project.catkin.pkg.version
}
subprojects {
/*
See https://github.com/rosjava/rosjava_bootstrap (look for gradle_plugins)
to see what is going on under the hood.
*/
apply plugin: 'ros-java'
}