Import Java source code as library in IntelliJ IDEA - java

I've just started Android development coming from a strong web and iOS development background. One thing I don't really understand is how to best import third party open source projects in my source.
In web development most projects has a bower package and in iOS most open source projects has a podspec that makes it really easy to import. But I don't see this as much in Java projects.
So, now I want to use jess-anders/two-way-gridview. What would be the best way to get this into my own app. I would very much like to avoid having to mix the third party source code and res files with my own.
I use IntelliJ IDEA.

If I understand correctly, then what you need is Maven, which is used for dependency management in Java projects.
You define your dependencies inside a pom.xml file. Here is an example of such pom file (see the < dependencies > tag ).
Generally, when you need to use a 3rd-party library, you look it up in the Maven Central Repository, from which Maven looksup by default when you specify a dependency, and copy the < dependency > element into you pom.xml file.
NOTE: I don't know why, but right now the Maven site is down. You'll have to check back later. In the meantime try looking for some tutorials on google.
If you need to import libraries which aren't in the Maven Repo, then you need to go to File>Project Structure>Libraries and click on the '+' symbol and add your .jar files from the your filesystem.
You need to do this on every machine that needs to work on this project, and also include the jars in your final apk. I do not konw how to do the latter.

Related

How do I add a Maven dependency in VS Code for a simple Java project (i.e. an unmanaged folder without any build tools)?

I'm trying to build an AI model for the Mario-AI-Framework using the Deep Java Library (DJL). I'm using VS Code with the Java extension, as this is part of a larger project, mainly in Python. Now I have a Pytorch model trained and ready to go but I need the DJL Pytorch engine to load it in Java. The problem is, the only info I can find on how to import this thing uses Maven or they build it from source using Gradle. I'm not used to working with Java projects and importing libraries so I'm at a complete lost here. If anyone could point me in the right direction I would appreciate it.
You can always download the jar files stored in the Maven repository and put them manually on your classpath.
Remember to do so recursively for all artifacts referred to until you have all the jar files that Maven would have downloaded for you.
For instance if you need commons lang you can get 2.1 from https://search.maven.org/artifact/org.mod4j.org.apache.commons/lang/2.1.0/jar - note the Downloads link in the upper right.
You might find it easier to rework what you have into being a Maven project, so your tooling does it for you.

How do I make use of a third-party java library github project?

When there's no obvious compiled jars provided on a github project page, I've been downloading the clear code and kludging them into subdirectories in my codebase to make use of third party code (or, most of the time, just not using the code at all). I know that's not the right way to go about this; what is?
There's a popular post about importing github projects into Eclipse:
Importing a GitHub project into Eclipse
...and I've recently learned how to make my own projects depend on one another. Is this the practice, then, to fill up your workspace with projects you're not actually going to work on, so that you can declare dependencies? I remember making dependency references to web resources in the past (itch.io, maybe?) in some configuration, where I didn't have to go download the code. Is there a more elegant solution like that for github?
I'm using Eclipse, and the current library I'd like to use is: https://github.com/FasterXML/jackson
If you're trying to just use FasterXML, the jars are hosted on MvnRepository as well as a lot of other projects.
If a github project's artifact isn't available on MvnRepository or some other online repository you could potentially clone, build the war/jar file yourself, and then manually import it into your project instead of copy and pasting code directly into your project. This would give you access to the library.

How do I check installed JARs, external libraries, etc. on three different Java IDEs?

I've written programs in several languages and have tutored students in computer science, but just starting to learn Java on my MacBook. Regarding this question, I'd be happy with any answer that points me to available information or tutorials that address my question; I'm capable of understanding advanced things.
I've been searching for the right IDE for me as well as something I can use with my students, and I've tried IntelliJ, Eclipse, and VS Code. Along the way I've installed external JARs to provide extra capabilities, such as Apache Commons.
Things are getting confusing. I've lost track of how I got to the present state in each IDE. I'd like to understand better how to know the overall Java environment that any given project is using on each of these IDEs, including any external JARs and where they are located. And I'd like to know if they borrow from the Java system environment.
My goal is to understand how my own system got to the way its currently configured, to update my configuration on a project-by-project basis, and to help my students get a matching configuration.
I'd also like advice on the right way, or simplest/cleanest way, to install external JARs.
Maven
Question: I'd also like advice on the right way, or simplest/cleanest way, to install external JARs.
If you really wanna work in a organised way and wanna focus completely on coding rather than looking for dependencies to work with , then try building your projects with Apache Maven. The magic wand of Maven projects are pom.xml file where all magic happens depending upon your wish.
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:
Describes and manages how software is built.
Describes and manages dependencies (various libraries used by your code).
Why Maven:
De facto standard
Able to compile, test, pack and distribute source code ( different Goals)
Robust dependency management (Most important from my point of view)
Extensible via plugin
Good community support and many fan boys around.
The big 3 IDEs (IntelliJ, NetBeans, and Eclipse) all having good
support for Maven, letting you use Maven as a substitute for their
own proprietary project definition and build process.
Maven famously caches all of its dependencies in the ~/.m2
directory, which is sometimes called the local Maven repository.
Maven local repository keeps your project's all dependencies (library jars,
plugin jars etc.). When you run a Maven build, then Maven automatically
downloads all the dependency jars into the local repository. It helps to
avoid references to dependencies stored on remote machine every time a
project is build.
You can simply deploy your project as JAR, WAR, or EAR file and use it on different IDEs or as standalone.
All IDEs need a way to know your project's dependencies. You can either tell them that yourself or let a build tool do that.
Manual dependency handling: by adding the jars to your project. This is probably the fastest way when working on a small project, with one developer, on a specific IDE, with few dependencies. Usually when telling the IDE that this .jar is a dependency of your project, the IDE stores that reference to a project-specific file (eg. in Eclipse the .classpath file which you can edit with a txt editor and see the dependencies yourself). However, it kind of locks your application to your IDE. Most IDEs have cross-IDE support for import and migration, but using both IDEs at the same time can be confusing when a dependency is added to one and has to be repetitively added to other as well. Furthermore, your dependencies have dependencies on their own. By adding manually your jars you are responsible to find and download their own dependencies as well.
Use a build tool: There are 3 standard such tools right now: Apache Ant with Ivy, Apache Maven and Gradle. All of them have support in the major IDEs for Java: IntelliJ IDEA, Eclipse and NetBeans. All of them use some extra build-tool specific files to store your project's configuration and subsequently configure your IDE and the IDE-specific files. That way, your project becomes IDE-agnostic, the IDE outsources the dependency handling to the build tool. These tools will download any direct or transitive dependencies of your project in a local directory or you can compile jars in a specified folder. From those, Ant is the oldest (with Ivy adding dependency handling support), Maven was developed after that and Gradle is the newest and probably the most flexible. In production however Maven is by far the most established one right now.
It would be also useful to look up the Standard Directory Layout. If you adhere to that, it will be easier to work/start with either Maven or Gradle.
Finally, you can search and find most of the free libraries in Maven-Central where conveniently their Ivy/Maven/Gradle script is added as well for you to use on your build-tool script. In many cases a .jar is provided as well if you prefer to manually add it as a dependency.
Regarding VS Code, I think it supports these tools through plugins but I'm not sure.

Dependency management in java

I implemented a tool and I want to make it open source.
The problem is that my tool has some dependencies (binaries/. jar files).
How can solve this problem, so that the person who downloads my source code, for example to extend it to a new feature, doesn't have to care of the dependencies?
Should i write the dependencies in the MANIFEST as relative path, relative to my source code or something like that?
You can convert your project into a maven project and then put your source code to githup or something similar. If your dependencies are not available on the web but instead your other projects, you should also place them on the web so maven can download them for anyone who wants to use.

How can I debug external JAR files

I'm using some apache libraries in my code. At the moment I'm hitting a variety of problems that I need to debug. However the problem is when I pass data into one of said Apache libraries, when I try to step into the method call using netbeans it simply does the processing in the background and doesn't step into the code I want to view.
Does anyone know how I configure the IDE so that I can debug the apache code as well? Right now I just associate the JAR's with the project by adding them as libraries.
You need to attach a source jar (e.g. commons-lang-2.4-sources.jar) to your dependency, instructions here
I should also recommend using Maven2 to manage your project dependencies, I've not used Netbeans for at least 5 years, but both Eclipse & IntelliJ have Maven plugins which will import all your dependencies and link them to sources/javadoc jars automatically.

Categories