Install a Java library without eclipse [duplicate] - java

This question already has answers here:
Including jars in classpath on commandline (javac or apt)
(5 answers)
Closed 4 years ago.
I need to use a library (JDOM) in my program, but I can't figure out how to use it without eclipse (I know on eclipse it is "build path" or something like that).
Can anyone help me please ?
Thanks

You do not install java libraries.
In the end, a Java library is nothing but one (or more) JAR archives that sit on your file system. Thus, "installing" boils down to:
identify the JARs to download
download them
put them in a known space in your file system
Then, when compiling/running a Java application that is supposed to use these libraries, you have to ensure that your classpath setup points to the correct JARs.
Typically, you want to use a Java library as part of one of your projects (a library does nothing for you, it needs to be used by a program to be useful). So the normal approach is that you have a project directory somewhere, and in there, you might have a libs subdirectory for example.
But I guess the real answer here is: learn about build system such as maven or gradle. These tools help you building java applications, and they also give you a "model" how to structure your java projects. And they take care of downloading libraries and such things, too.

Related

Deploying JARs with Java 9 and above (JDK11 in this case) (JLink confusion?)

I'm a Java veteran, but I've been using JDK8 for a long time. I've decided I finally want to upgrade, so I've been using Java11. I've been enjoying the new features, but I've gotten the point where I need to deploy my software.
In the past I would export a runnable JAR from Eclipse and bundle it with an appropriate JRE. Then I'd use both to run the software from an OS-specific program (e.g. an EXE file that fires up the bundled JRE with the given JAR). Of course, now this isn't really an option because JREs are a thing of the past. Supposedly the new system in its place is much lighter weight and straight forward, the only problem is I can't figure out how to actually use it.
I've been reading about how to deploy programs with JDK9 and above and have seen people mention JLink and link documentation to it, but I can't seem to find a straight answer on how to just simply export a runnable JAR. The thing is - I don't really need all of the module support and don't really want to have to configure it. Is there a tool for simply exporting something I can run? How is this done now?
Sorry if this is a dumb question, I'm just genuinely confused at how this all works and can't really find anything online that lays it out in a clear and concise way. There's a lot of documentation on JLink and what it does, but I haven't really found anything that explains the root purpose for all of it.
TL;DR; how do I export working Java programs with JDK9 and above using Eclipse (latest version)?
Thank you for your time!
...how to just simply export a runnable JAR.
Well, if you're not planning on using modules for your application classes, the JAR part should be the same as before. You basically just have to create your own JRE using jlink, e.g.:
jlink --add-modules java.se --output jre
That would create a jre folder with a runtime image that includes all the java.se modules.
You can then bundle that with your JAR like before.
The interesting part here is that you can pick and choose which modules go into this runtime image. java.se is an aggregator module that transitively includes a bunch of other modules. But you could also specify your own specific list of modules, leaving out some of the ones you don't need, making the final runtime image smaller.
I've spent literally months on trying to figure out how to create a single, clickable executable using Java 9+. I now conclude that it is impossible. Whether you use jlink directly yourself, or indirectly via Maven or Gradle, the result is that jlink produces a full directory structure that you are left to "distribute to your users" (somehow, I guess magically, since nothing in Java 9+ tells you anything about how you're supposed to do this). Then, once your user (somehow) has this directory on their machine, they are forced to invoke runtime-image-directory/bin/your-program-name. As though your user is a programmer who is happy to have to install a directory structure, and drill down to invoke a specific file name, which is buried in a directory with lots of other files, rather than being a user who USED to be able to simply double-click on a .jar file to run it. This makes me really wonder if whomever designed all this thought at all about the "user experience". The fact that there appears, as of Java 9+, to be NO WAY to simply deliver ONE file (such as a .jar) to users, together with the fact that just BUILDING this "runtime image" is fantastically complicated, means that developers are going to abandon Java in droves. It really seems to me that the brainiacs at Oracle simply didn't think this through. They have created a death knell to Java by making something onerous for developers to build and distribute, and onerous for users to invoke. I don't see how this situation can be allowed to remain without Java ultimately dying off. Somebody please correct me if I am missing something here, but I've spent months now trying to figure out how to create a runnable jar in Java 9+, USING MODULES, and it appears there is no way to do it.

How do I set the classpath on Mac and is it necessary? [duplicate]

This question already has answers here:
Mac User - How do I set CLASSPATHS in Mac (I'm working on a Lucene Demo)
(3 answers)
Closed 7 years ago.
I'm a complete beginner to Java, and initially downloaded and installed Android Studio, latest JDK and SDK libraries and tried to follow the "Build My First App" tutorial through developer.andoird.com before realizing I need to stick to basics and learn Java first.
Now I'm trying to follow a Java Beginner's tutorial which said it explains the process for setting up Java on PC, Mac and other platforms but is only explaining it for PC. I'm stuck on how to set a classpath as the tutorial explains it for PC only.
I'm so sorry if these are basic questions, I couldn't really find anything in past forums that explained it in beginner terms that I could understand. What is a classpath and why is it necessary to set it up? How do I set it for Mac? I read a number of forums saying you don't really need to set one or that it's not necessary, while others imply otherwise.
*edit: to clarify, I'm taking the Udemy Java Programming for Beginners, stuck on the setting classpath and checking classpath part of the tutorial prior to learning how to execute the basic Hello World.
The class path is a setting passed to the Java runtime. It's basically a list of folders for the Java runtime to look for class files. The simplest class path is the current folder:
java -classpath . MyClass
Java will execute MyClass.class if it's in the current folder. You will need more complex class path setups when your project gets more complex (when you start to use libraries). For starters you can stick with the current folder.
CLASSPATH is what you use to tell the JVM how to find the .class files it needs at compile and runtime. Yes, you need it for all operating systems.
The best way to do this is using the -classpath option on javac.exe and java.ext.
When you start creating and deploying Java EE projects on an app server, you'll learn that they have conventions for CLASSPATH that you'll need to know and follow when you package your application (e.g. WAR files assume WEB-INF/classes is head of CLASSPATH; all JARs in WEB-INF/lib are added to CLASSPATH for you).
You don't need an environment variable CLASSPATH to be set. I would discourage you from having one, since it changes with each and every project you run.

How can I make a program executable by double clicking? [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 8 years ago.
I wrote a JAVA program for my wife to compare coupons stacked with sales on different item sizes to save her time on her shopping-trip-planning. Now I need to put it on her computer in such a way that she can double-click something, and have the program run. I'm not finding anything good on the interwebs (like a decent step by step).
It is a simple program, one class with a nested class, and various imports of swing, awt, and text.
Since it's my wife's computer, I can make sure that she is running the appropriate JRE. Thanks in advance for any help.
You can use Launch4j application. That convert executable jar file to windows native executable application.
You can also embed JRE with it so if JRE is not installed on machine the application will run.
you can also add some other mechanism to it also like loading image, icon, etc.
I assume you are using Eclipse. In this website they given it very clearly for eclipse.
Create Executable files in eclipse
Export it to (or create a) executable Jar.
Most IDE's will do this (I think you have to dig around Eclipse a little) or take a look at Packaging Programs in JAR Files and Setting an Application's Entry Point if you want to do it by hand
Most GUI OS's will run the Jar via a double click.
You could also investigate generating a native executable wrapper, which will make the program "look" more familiar to the user, including launch4j or exe4j or Packaging a Java App for Distribution on a Mac depending on your target platform
Use NetBeans and simply 'Build' the jar file. This will produce an executable file within the 'dist' folder of your project.
Eclipse is the same however slightly more hidden.

how can i add .class files to my java project in eclipse [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I include .class files in my project in Eclipse? (Java)
I am using Eclipse for Java. I have a school assignment and I have to add some .class files to my project. how can i do this. and is there anything else that i have to do. I read somewhere i have to create some Interfaces. I am not sure. help me out you big guys
Right click on the project. Then go in "Properties".
Then click on "java build path".
The only way that i know to add class files is to add them as a .jar file by adding them to the "Librairies" list that you will find there.
If you do like that it will work.
To use .class files (if you have them) in a 'clean way', assemble them in a jar file (Java ARchive) and import that as a library. As soon as you do it, you get access to your classes easily.
If you want to get/see the implementation of .class files use Java Decompiler, and you can get it as a standalone GUI or as an eclipse plugin :
JD-GUI
JD-Eclipse
Enjoy :)

How do you use a Java Library?

I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.
I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.
If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?
And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?
I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.
Most java problems are classpath problems.
Have you included those libraries in your classpath?
If you are using eclipse, you could
Project - > properties -> Java build path ->addJar.
And the Jar file should be placed in a directory inside your workspace (lib/ for example)
If you have to take your project to another computer, you could take these steps
Before doing anything, export your project (as a Jar file, for example).
Save it into your favorite drive (cd / usb drive/ diskette/ tape).
On "the other" computer, you can import this project into your workspace
In Eclipse, you need to add libraries to the project build path.
In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:
javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java
These dependencies would also be required to invoke the app:
java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass
This page gives some good info, though googling for the term "classpath" should provide alternative sources.
You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.
You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.
Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.
Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

Categories