Adding a library to a netbeans gradle project - java

I have decided to try and use libgdx for graphics. However it uses gradle to manage the package; I however have very little experience with gradle.
My question is, if I have a library (.jar file) of my code. Normally in netbeans I can right click 'libraries' and choose 'add jar/folder' and it adds the library to my ant file and more importantly the ide now recognizes the .jar and exposes it to the auto-complete etc.
On gradle I have only gotten the file to show up in the 'dependencies' branch by putting the following in the build:
dependencies {
runtime fileTree(dir: 'libs', include: '*.jar')
}
However the libraries won't import, nor does the ide recognize their existence. The (absurdly large) online documentation for gradle seems to only want to deal with repositories and I can not find documentation for netbeans gradle. Any help would be greatly appreciated.

This one stumped me for a while as well...
First manually add the dependency to your build.gradle script. Like your example:
dependencies {
runtime fileTree(dir: 'libs', include: '*.jar')
}
Then, in Netbeans's "Projects" side-panel, right click on the project name and select "Reload project" from the context menu.
Your dependencies should then show up under the project's Dependencies folder, and code completion, compile error checking, etc. should work as usual.
If you cannot see the "Projects" side-panel click select from the menu bar Window -> Projects or press Ctrl + 1.

In netbeans 11 it looks like the reload project option has been moved to right-clicking on 'Configurations' in the projects root folder and selecting 'Download Sources'

Related

Debugging tomcat filter chain in intellij [duplicate]

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?
Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option
IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...
If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)
You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.
I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.
Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library
Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries
While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.
File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class
You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:
If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

How to import a jar package into my IntelliJ [duplicate]

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?
Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option
IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...
If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)
You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.
I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.
Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library
Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries
While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.
File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class
You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:
If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

Installing additional Java dependencies to react-native library

I want to bring more functionality to an existing react-native library - namely to react-native-maps. In order to do that, I have to add an additional Java library. No I wonder where exactly am I supposed to add the Java library. Do I add here (in a newly created lib folder)
https://github.com/react-community/react-native-maps/tree/master/lib/android or somewhere else? How do I make it "known" to Java?
I think you mean with "Java" your IDE. Depends on what IDE you're using if you happen to use
IntelliJ it goes as follows:
Click File from the toolbar
Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Dependencies tab
'+' → JARs or directories
Eclipse
Start Eclipse, and locate the project folder to which the library
should be added.
Right-click this class folder, and select "Properties"
Select "Java Build Path" on the left, and then the "Libraries" tab.
Now, click the "Add External JARS..." button
Locate and select the .jar file you just downloaded, and then
click "Open"
Finally, click "OK" to close the dialog box.
Netbeans
In the Projects window right-click on the name of the project
Click Properties, The Project Properties window opens.
In the categories tree select "Libraries"
On the right side of the Project Properties window press button "Add
JAR/Folder"
In the case of Gradle you should edit the build.gradle file located at the root of the project. In that file under the section dependency.
Copy From Source
Gradle Guide Android Studio 'Adding dependencies'
Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
Because Gradle reads paths relative to the build.gradle file, this tells the build system to add all JAR files inside your project's module_name/libs/ directory as dependencies.
Alternatively, you specify individual files as follows:
compile files('libs/foo.jar', 'libs/bar.jar')
Example
dependencies {
def googlePlayServicesVersion = rootProject.hasProperty('googlePlayServicesVersion') ? rootProject.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
def androidMapsUtilsVersion = rootProject.hasProperty('androidMapsUtilsVersion') ? rootProject.androidMapsUtilsVersion : DEFAULT_ANDROID_MAPS_UTILS_VERSION
compileOnly "com.facebook.react:react-native:+"
compile files('libs/name_of_library.jar') //<----- added external library for gradle to build
implementation "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
implementation "com.google.android.gms:play-services-maps:$googlePlayServicesVersion"
implementation "com.google.maps.android:android-maps-utils:$androidMapsUtilsVersion"
}

Importing a JAR into Android Studio 2.2

I'm trying to import a JAR that I made in Groovy into an Android Studio 2.2 project and all the instructions I can find say to just add it under Project Structure -> app -> Dependencies. I did this and it's still not letting me use the imported JAR. I've imported JAR files from Groovy into other Java projects in Eclipse. Did something change in Android Studio 2.2 that changes how you're supposed to do this?
I thinnk this step help You
1 ) Put the jar (in my case, yourJar-2.2.4.jar) into the libs folder.
2) Right click it and hit 'Add as library'.
3) Ensure that compile files('libs/yourJar-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files).
4) Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system.
5) Clean Project.
6) Run.
May be it,s Working
I figured it out but I'm still not sure why it works. I changed the package name of the JAR to match the Android project. My Android project is com.johnwstanford.simplegps and I originally just called the JAR package SimpleGpsOutputFormatter. I changed it to com.johnwstanford.simplegps.SimpleGpsOutputFormatter and for some reason, that made it work. Anyone know why that would make a difference?

How to Add JAR (HTMLCleaner) to Project in Android Studio 1.1?

I've spent countless hours trying to add a simple JAR (HTMLCleaner) to my project in Android Stuio 1.1 to no avail. I imported the JAR as a module through the interface (File > New Module > Import JAR...), added it as a dependency on my app module, and even reference it in my AndroidManifest.xml <uses-library etc...>.
When I try to "import org.htmlcleaner;" in any of my classes I get a "cannot resolve symbol error." I've researched and tried every suggestion in every permutation and combination. Can anyone offer additional direction or a step-by-step tutorial on the proper way to do this?
Remember that Android Studio uses Gradle as a build tool, and part of the build flow is to handle dependencies. That being said, you have to specify to Gradle that includes your external jar files, making the following modifications:
Open build.gradle file of your module project (generally called "app")
Inside "dependencies", add compile fileTree(dir: 'libs', include: ['*.jar'])
Create a folder called libs inside your module app.
Copy your jar file inside of it
Run the build process of Gradle clicking in the "Sync now" link of Android Studio, and now your library should be recognized.
You can add jar file by following Rodrigo Ayala answer. Also Instead of using jar file you can also add the dependency in your project by add the following lines to build.gradle
dependencies {
// your other dependencies
compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.16'
}
Inside your "app" module create a new folder "libs".
Paste your jar file inside libs folder.
Right Click on the jar file and select "Add as library".
DONE.

Categories