Unable to build gradle project in eclipse? - java

After cleaning when i try to build gradle i get an error in the console saying:
package org.json does not exist import org.json.JSONObject;
cannot find symbol
symbol : class JSONObject
there are red marks in the java file at all places where jsonobject and json array exists.
I have put the folder web inf/lib that contains all the jar files inside the src/main/webapp directory that i have created.
currently the contents of my build.gradle file are:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'i2cdev001' at '14/11/18 3:11 PM' with Gradle 2.14.1
*
* 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 guide available at https://docs.gradle.org/2.14.1/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'war'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Note: Also in the project properties i am unable to see any jar files under the Web App Libraries in the java build path tab. I can see only access rules:no rules defined and native library locations:(none)

As your project is gradle project, Adding jar manually wont work.. You have to mention path where you have kept all your jar files in your build.gradle file.
Mention your jar file path in repository under flatDir {}:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then you have to add which jar from that folder you have mentioned above (ie libs)
dependencies {
compile 'gson-0.1.0'
}

Related

How can I add an external folder of java code to my gradle project with a relative path?

I have a Gradle Java project that I want to reference other java code that exists in another repo. I am not quite sure how to do this.
My existing build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
// implementation project(':project1')
// implementation files('../../project1/lib/build/libs/lib.jar')
}
application {
// Define the main class for the application.
mainClass = 'project2.App'
}
What can I add to this file, to have it reference a relative path that points to a folder with java source files in it?
Well, According to the discussion in the comments, here is how to do it.
project1 has its own build.gradle and setting.gradle
go setting.gradle and add the following after cloning the repo inside project1 folder:
include ':project2'
Now, In your project1 in build.gradle file You can add project2 to dependencies section so it will look like this :
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
implementation project(':project2')
}
the final result :
Any time you build project1, project2 will be built as well.
Any update for project2 will be effected immediately to project1 after any build operation.
Might be helpful to read official docs about multi project builds.
If you don't want to clone project2 inside project1, Then build project2 with gradle command line gradle build
This will result in a Jar file created at project2/build/libs/project2-0.0.1-SNAPSHOT.jar
Then you can add it to project1 with a relative path to this Jar like this:
implementation files('../project2/build/libs/project2-0.0.1-SNAPSHOT.jar')

OpenCV with Java Gradle Project as fat jar

I am actually Trying to use OpenCV in Java based Gradle Project. Since, OpenCV needs native library and Jar File for execution. I am trying to wrap native library and Jar together using gradle, but I am facing errors in doing so.
When I try to run project, project is not able to find native library for opencv jar and giving me below error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java340 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at Library.(Library.java:9)
Although, I know how to set native library manually in Gradle project but I am not sure how to do it via Gradle and wrap native library in fat jar. Here is my build.gradle
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
configurations {
// configuration that holds jars to include in the jar
openCVLibs
}
dependencies {
openCVLibs fileTree(dir: 'libs', include: '*.jar')
openCVLibs fileTree(dir: 'libs', include: '*.so')
configurations.compile.extendsFrom(configurations.openCVLibs)
}
jar {
from {
configurations.openCVLibs.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
}
have also included link of sample eclipse project
So Here is edit
Based on #kerry's comment I tried to crate mvn artifact following openCV Maven, but now I am facing following error while creating mvn build
[ERROR] Failed to execute goal
org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
(set-arch-properties) on project opencv: Properties could not be
loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to
execute goal
org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties
(set-arch-properties) on project opencv: Properties could not be
loaded from File: /media/nitish/8EE47633E4761E21/opencv-3.4.0/build/build.properties
There is no build.properties file present in build folder. Since build folder is created by maven task only, so build.properties file should be created by maven only.
Following there is a working example of a build.gradle file. Make sure to read the comments and make changes when appropriate.
By running gradle fatJar you can create a working Java Jar of your application with OpenCV inside.
However, apart form including your OpenCV library in your Java Jar, you will need to load the OpenCV native file at the beginning of your code.
They are two ways to do that:
1) Load the file by providing the full path:
System.load("my/full/path/opencv.dll");
2) If your native file is located inside your Java Library Path:
System.loadLibrary("opencv");
Take notice that in the second case you only need to provide the name of your native file (without its extension).
The default Java Library Path depends on OS:
On Windows, it maps to PATH
On Linux, it maps to LD_LIBRARY_PATH
On OS X, it maps to DYLD_LIBRARY_PATH
If you want to set your own Java Library Path:
try {
System.setProperty("java.library.path","YOUR/PATH");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception ex) {
System.out.println("Failed to set Java Library Path: " + ex.getMessage);
}
build.gradle
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = "YourMainClass" // You Main Class name
repositories{
mavenCentral()
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude "build/libs/philipath/**"
}
with jar
}
artifacts {
archives fatJar
}
dependencies {
// Local libraries
compile fileTree('lib') // assuming there is a folder named 'lib' in your project root with your OpenCV jar inside
// other dependencies
}

How to setup a Java Gradle plugin project in IntelliJ?

I'm having issues setting up a Java Gradle Plugin project for IntelliJ.
Specifically, I can't get the Java to import the required gradle library.
import org.gradle.api.Plugin;
import org.gradle.api.Project;
I found the answer for Groovy and ported it over for Java.
Insure you have gradle downloaded, and the gradle bin directory added to your path.
Create a new directory for your project to exist in. Open up command prompt, and run the following command:
gradle init --type java-library
Then edit the generated build.gradle file and add the following the the dependencies:
compile gradleApi()
Also and the following:
apply plugin: 'idea'
This should result in a build.gradle that looks like:
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'idea'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
compile gradleApi()
// The production code uses Guava
compile 'com.google.guava:guava:20.0'
// Use JUnit test framework
testCompile 'junit:junit:4.12'
}
Then back in command prompt, run:
gradlew idea
And open the generated project in IntelliJ
Groovy Source: How to setup a Gradle plugin project in IntelliJ?

No Main Maifest attribute Gradle

I have read all the other threads about this problem and applied all the solutions I could find. Nothing helped. When I run the gradle.build task I get a .jar file. But when running the file I get no main manifest attribute, in DiscordBotJDA-1.0.jar
Can annyone provide help?
Thanks a lot!
Here is my gradle.buidl file:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'Timbo' at '7/13/16 2:08 PM' with Gradle 2.9
*
* 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 guide available at https://docs.gradle.org/2.9/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'start.StartUp'
version = '1.0'
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'MCI_Bot',
'Implementation-Version': version,
'Main-Class': 'start.StartUp'
manifest.attributes("Main-Class": 'start.startUp')
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'net.dv8tion:JDA:2.1.3_327'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
I am using eclipse in case it matters.
Were you expecting that your "fatJar" task would be doing that? Just defining the task doesn't put it into the task execution tree. You have to either run the task directly, or specify a task dependency relationship including your task. For instance, saying that another task depends on it.
You might be better off just specifying a "jar" configuration block, which will configure the already existing "jar" task, which already has proper dependency relationships defined.
Read the User Guide for examples of configuring the "jar" task.
It might be more convenient to download the Gradle distribution, which provides the PDF of the User Guide, which might be easier to search.

Why do xml configs of gradle dependencies go with "Gradle__" prefix? Intellij IDEA 13

Java Spring project with Gradle 1.9 and vertx. Local gradle distribution.
Some lines of build.gradle
apply plugin 'java'
apply plugin 'groovy'
apply plugin 'idea'
buildscript {
repositories {
jcenter()
}
}
repositories {
mavenCentral()
}
dependencies {
...
compile 'org.springframework:spring-context-support:3.2.5.RELEASE'
compile 'org.springframework:spring-aop:3.2.5.RELEASE'
compile 'org.springframework:spring-aspects:3.2.5.RELEASE'
...
}
I have an existing gradle project downloaded from git with xml configs in .idea/libraries folder named spring-aop_3_2_5_RELEASE.xml, for example, where we can find xml tag <library name="spring-aop-3.2.5.RELEASE">...</library>.
After I had imported this project new file Gradle__spring-aop_3_2_5_RELEASE.xml appeared with only difference in name attribute of the library tag: Gradle: spring-aop-3.2.5.RELEASE. So i have duplicate xml configs for dependencies. I wonder why my gradle added that prefix.
The prefix is hardcoded, IDEA 13 needs a reimport of your old Gradle projects that were created in IDEA 12. It's not obvious, but there will be a notification about it in the next update.
In the Gradle generated project you can exclude the library files from the version control, same for the .iml files that can be also ignored when using Maven. Other files can be still shared (like code style, run configurations, inspection profiles, etc). Check this document for details.

Categories

Resources