Automate: export runnable jar, by Launch Configuration, Eclipse - java

I develop Java code using Eclipse. When I want a runnable jar I point and click in the GUI like this:
File->Export->Runnable Jar -> Launch Configuration (select MainTest) --LibraryHandling (extract required libraries into generated JAR)
I want to do this automatically, how can I do this in bash?

Depending on what is in your project and what dependencies you have, this could be pretty complex using the command line tools Java provides. The Java tutorial is pretty good in explaining the usage of the related tools that ship with Java to create jars, but I think most people suggest using a build tool like Maven or Gradle for command line builds.
If you're using the jar tool though, the most basic example for the jar tool with a runnable Main class being specified is:
jar cmf existing-manifest jar-file input-file(s)
where existing-manifest would be your manifest file that specifies your main class.

Related

How to export JavaFX gradle project as a standalone executable file for deployment?

I have completed my JavaFX application within gradle build system, and it is working fine in all way. Now I want to export as a .EXE file for standalone software distribution, I tried much more tricks but no gain. If some one can help me out to wrap my project in a software setup, It would be grateful.
Follow these steps to export your JavaFX project into executable Jar
Goto> Project Structure
Goto>>Artifacts
Click "+">> To add new artifact
It will shows a dropdownlist
Select>>Jar>>From module with dependencies
You will see a nested window as shown
Select Main class of your project
Check In the Option " Copy to output directory.." >>Ok
Goto>> Menu-bar>>Build>>Build Artifacts
Select>> your Project.jar>> Build
This will create executable jar file in your project source folder
Locate your jar file in path project\out\artifacts..
Now you can run this jar file simple cmd commad or with batch file
Cmd Command>> Java -jar project.jar
Using batch file>> make .bat file name it "RUN" and write these commands inside
Specify the Java Runtime path and "Javafx Sdk path" along with VM
options & Project Jar
Run your standalone application .. Enjoy ;)
Creating an installer for the desktop platforms (Windows, macOS, Linux) has become easy these days. The tool of choice is jpackage which started to be shipped with JDK 14. It can either be used on the command line on the finished project or you can use a Gradle plugin (https://github.com/beryx/badass-jlink-plugin). If your project is not modularized you could follow this tutorial https://github.com/dlemmermann/JPackageScriptFX which also uses jpackage but together with Maven and some other tools from the JDK. The Maven part could easily be rewritten to Gradle, if needed.

exported Libraries handling in exported jars java

I have have a java project in eclipse that uses a bunch of jars.I want to export the class as a runnable jar.When i export the project with jar libraries extracted in the jar it gives me an error when running the jar.
(UncaughtExceptionHandler in thread "main")
However when i exract the libraries as a subfolder besides the runnable jar it works fine.
How can i make the project work the the libraries used extracted in the jar and not in a subfolder?
Thank you
Eclipse has an option to package required library's into the generated JAR. If that is selection that should fix your problem.
Correct option
If I understand you correctly, you're having trouble to export your program as a Jar file including the dependencies libraries.
So, the solution is like this:
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
launch configuration dropdown is populated with classes containing a public static void main(String[] args) method. The selected class is started when you run the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project.
If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included.
Also, there another solution using Gradle, which is much easier and robust, which you can manage all your dependencies so easyly and do so much more.
You can read more here.

How to export Java app as runnable?

I created a Java project called TotalBeginner, and exported as a jar. I then reference it in a desktop app with a SWT GUI, called MyLibrary. I now want to be able to run MyLibrary outside of the Eclipse IDE (I am running Luna 4.4.0). In following the advice of other answers to questions on Stack Overflow, I export as Runnable JAR File. I pick "Package required libraries into generated JAR" - so if I understand correctly, referenced libraries like TotalBeginner.jar should be included in the MyLibrary.jar, correct? However, when I run it, it returns to the command prompt with absolutely nothing appearing to happen. Task Manager (Windows 7) shows no Javaw process. What am I missing? Thanks.
C:\Users\jimerman\>javaw -jar MyLibrary-app.jar
C:\Users\jimerman\>_
No errors, no dialogs.
I suspect in your JAR you only have classes of your own project (which is fine in fact) and you haven't put all dependent JARs in classpath (As it is complaining for unable to find org/eclipse/swt/events/DisposeListener)
It may be tedious to find out all dependent JARs and put it in classpath of java command manually.
Consider making use of build tools like Maven and Gradle, which will save you trouble in collecting dependencies, and there are plugins for them to help you to construct artifacts that makes execution easier.
For example by using Maven, what you need is to prepare a POM, put SWT (and other dependencies) as dependencies of your project.
Then by making use of shade, appassembler or assembly plugins, you can easily have a uber-jar that contains all dependencies, or have a zip files that all dependencies are put in a specific directory and you can easily execute using generated command.

How to build a distributable jar with Ant for a java project having external jar dependencies

I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.

Is there a way to export a Makefile from a Java Eclipse project?

So I have this Java project made up of several classes, some external JAR files and an executable Java program. I would like to export the whole code and external JARS to an external directory and to produce a Makefile to build the program with all the dependencies. Is there an automated way to do it?
Thank you
Tunnuz
I think I understand the question. Of course if you use an external build system like maven or ant, then we are decoupling the build process from the IDE. (But in some cases the IDE does integrate pretty closely with the build tool.)
But if you want to continue building using eclipse and to generate an ant file one fine day, then there is a tool for that. Its called EBuild. It leverages all the classpath information that eclipse already has and builds an generic ant file out of it.
Do you use maven?
If so this can be easily achieved with maven assembly.
If not, you can use ant to bundle exactly what you need.
When you right-click your project in Eclipse, there is an option called "Export". It can create build.xml for ant for your project.

Categories

Resources