Firstly this is a very n00b question. But being a junior dev I've never needed to import and work with other Java Frameworks. The standard library was always good enough for me to write the classes I needed to write.
But now getting exposed to more "advanced" concepts, I need to start working with external frameworks, e.g. JSON for Java, Apache's HttpClient for java and so on. And I'm looking for a basic understanding on how this works and how to go about importing these libraries so you can start working with the classes...
So my initial understanding is that each of these fraemworks will provide you with a .jar file that contains all the classes for the framework. Which you then import into your project and lo and behold you'll be able to use the classes/library in your project by just importing it e.g. 'import org.json.*;'
Is the understanding correct?
Correct.
You just add the libraries to your classpath and are now able to use classes from these libs.
How you add the libs to your classpath depends on your actual development environment. If you use Apache Maven for example, you just have to define the dependencies (libs) in your projects pom.xml and Maven downloads them automatically for you.
hth,
- martin
EDIT: The following only applies if you are not using automated build-tools like Maven or Ivy
Yes this is correct. To use a third party .jar file, download and place it in a convenient location (either system-wide or project-specific depending on your needs) and then include it in your classpath.
When executing from the command line do:
java -cp /path/to/library:. path.to.main
The :. is necessary so that the JVM will find your main method.
In an IDE you should be able to include the library in your classpath via the options menu.
Then you can just use the third party library like any other:
import name.of.library.class;
//Do something
Related
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.
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.
I have to write a java application which I'm putting together using eclipse and it relies on open source code. This application needs to be self-contained, meaning that I'm supposed to create a jar file that has no external dependencies.
I can use the open source code when I reference the jar files in the project's build path, but the idea is to have the actual source code as part of the eclipse project, side-by-side with my code.
The source code can be found here: http://hc.apache.org/, but when I import an existing file system into my project I can't quite get things to work. The packages end up with the wrong names, breaking references, and I can't do anything. Notice that the folder containing the source code has this structure:
httpcomponents-client-4.2.3\
src\
httpmime\
httpclient-osgi
httpclient-contrib
httpclient-cache
httpclient-benchmark
httpclient
fluent-hc
each of those subfolders has src/main/java/org/apache subfolders.
Can someone please explain how to do this? Am I supposed to import everything one java file at a time?
Use a tool like OneJar, FatJar, JarJar, etc. to create a single-jar application.
As Charlie mentioned, the Maven Shade plugin is another choice, particularly if you're already using Maven. If you're not, consider it or another transitive dependency management tool.
Some tool should be used, IMO, and it's more important the more dependencies you have.
Alternatively you could use a jar class loader and include the jar file in your artifact.
I would most definitely not include the source of dependencies in your own project.
Let's say you create a new project, and want it to make use of some 3rd party library, say, widget.jar. Where do you add this JAR:
File >> Project Properties >> Libraries >> Compile-Time Libraries; or
File >> Project Properties >> Libraries >> Run-Time Libraries; or
Tools >> Libraries (Library Manager) >> Library Classpath; or
Tools >> Java Platforms (Java Platform Manager)
All of these dialogs seem to do the same thing but I'm sure they all have their proper usages. Can't find a good "best practices" article online and the NetBeans Help Contents dialog isn't helping with this either.
Right click 'libraries' in the project list, then click add.
You want to add libraries to your project and in doing so you have two options as you yourself identified:
Compile-time libraries are libraries which is needed to compile your application. They are not included when your application is assembled (e.g., into a war-file). Libraries of this kind must be provided by the container running your project.
This is useful in situation when
you want to vary API and implementation, or when the library is supplied by the container (which is typically the case with javax.servlet which is required to compile but provided by the application server, e.g., Apache Tomcat).
Run-time libraries are libraries which is needed both for compilation and when running your project. This is probably what you want in most cases. If for instance your project is packaged into a war/ear, then these libraries will be included in the package.
As for the other alernatives you have either global libraries using Library Manageror jdk libraries. The latter is simply your regular java libraries, while the former is just a way for your to store a set of libraries under a common name. For all your future projects, instead of manually assigning the libraries you can simply select to import them from your Library Manager.
If your project's source code has import statements that reference classes that are in widget.jar, you should add the jar to your projects Compile-time Libraries. (The jar widget.jar will automatically be added to your project's Run-time Libraries). That corresponds to (1).
If your source code has imports for classes in some other jar and the source code for those classes has import statements that reference classes in widget.jar, you should add widget.jar to the Run-time libraries list. That corresponds to (2).
You can add the jars directly to the Libraries list in the project properties. You can also create a Library that contains the jar file and then include that Library in the Compile-time or Run-time Libraries list.
If you create a NetBeans Library for widget.jar, you can also associate source code for the jar's content and Javadoc for the APIs defined in widget.jar. This additional information about widget.jar will be used by NetBeans as you debug code. It will also be used to provide addition information when you use code completion in the editor.
You should avoid using Tools >> Java Platform to add a jar to a project. That dialog allows you to modify the classpath that is used to compile and run all projects that use the Java Platform that you create. That may be useful at times but hides your project's dependency on widget.jar almost completely.
Project Files Services Tabls
go files tabs
drag drop file to libs files hover.
return project tabs and what are you see :)
I have a Java project that currently has a lot of JARs in its libraries directory, which are all included in the resulting package when building. I know, however, that some of these libs are never referenced in the project.
Is there a tool that can search for libs that are not referenced within the project? I guess there must be something in that sense.
BTW, an Eclipse plugin would be awesome.
EDIT: I chose to go with ClassDep because it was the only suggestion that worked. However, I'm having some trouble with it: please check this question
Beware of the case that a class is loaded via Class.forName() and not specified as a dependency in the manifest file (there is a Depends-On: attribute that is for that, but many people don't specify it, which breaks tools like this, the bane of my existence when I worked on such a tool).
ClassDep (from Sun, in the Jini development kit) will do this for you.
ClassPathHelper can help you with that.
Espacially the "Not on Classpath View"
This view scans for jars that are not on the classpath (but are under the current project). It provides basic browsing of packages and classes that are available but not on the classpath. This can be helpful when trying to build up a classpath, as you can quickly browse for the missing classes to see which jars contain them.
Not an eclipse plugin, but I believe the "shrinking" functionality of ProGuard is exactly what you're looking for.
I wrote a small eclipse plugin that takes an existing java project from the workspace. For every classpath entry of the projects raw classpath it removes it from the projects raw classpath and builds the project. If no problem markers with severity error appear on the project, it permanently removes the classpath entry from projects raw classpath.
I'm not able to share that plugin, but that is not too much work to implement it yourself with the links to the api given above.
You also can't tell if JARs that you don't import are required dependencies of dependencies. For example, if you use Spring it comes with its own dependencies, even if you don't import or call those classes in your code. I'm ignorant of ProGuard - does it check for those cases?