This question already has answers here:
Avoid error when running java tests which generate super long command lines?
(2 answers)
Closed 9 years ago.
I've a Java console application with a lot of dependencies. I've to run it in production, so I created a runnable jar. The fact is that in order to run it, I need to write something like this:
java -classpath C:\Users\dandini\workspace\detror\target\classes;C:\Users\dandini\.m2\repository\mysql\mysql-connector-java\5.1.10\mysql-connector-java-5.1.10.jar;C:\Users\dandini\.m2\repository\org\hibernate\hibernate-entitymanager\4.2.4.Final\hibernate-entitymanager-4.2.4.Final.jar;C:\Users\dandini\.m2\repository\org\jboss\logging\jboss-logging\3.1.0.GA\jboss-logging-3.1.0.GA.jar;C:\Users\dandini\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\dandini\.m2\repository\org\hibernate\hibernate-core\4.2.4.Final\hibernate-core-4.2.4.Final.jar;C:\Users\dandini\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\dandini\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.1_spec\1.0.1.Final\jboss-transaction-api_1.1_spec-1.0.1.Final.jar;C:\Users\dandini\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.0-api\1.0.1.Final\hibernate-jpa-2.0-api-1.0.1.Final.jar;C:\Users\dandini\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.2.Final\hibernate-commons-annotations-4.0.2.Final.jar;C:\Users\dandini\.m2\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;C:\Users\dandini\.m2\repository\commons-dbcp\commons-dbcp\20030825.184428\commons-dbcp-20030825.184428.jar;C:\Users\dandini\.m2\repository\commons-pool\commons-pool\20030825.183949\commons-pool-20030825.183949.jar;C:\Users\dandini\.m2\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;C:\Users\dandini\.m2\repository\org\slf4j\slf4j-api\1.6.6\slf4j-api-1.6.6.jar;C:\Users\dandini\.m2\repository\org\slf4j\jcl-over-slf4j\1.6.6\jcl-over-slf4j-1.6.6.jar;C:\Users\dandini\.m2\repository\org\slf4j\slf4j-log4j12\1.6.6\slf4j-log4j12-1.6.6.jar;C:\Users\dandini\.m2\repository\log4j\log4j\1.2.15\log4j-1.2.15.jar;C:\Users\dandini\.m2\repository\commons-httpclient\commons-httpclient\20020423\commons-httpclient-20020423.jar uk.co.dandini.Detrot uk.co.dandini.Detrot
Isn't there a shorter way? How is it usually deployed and run in production?
Usually it's packed into an executable JAR which contains a manifest with a classpath which Java uses to load classes: How To Make A Java Exe File Or Executable JAR File.
Or you could created your own loader which sets the classpath for your app as it's done in Maven AppAssembler plugin (recommended).
You are using maven, so it's very easy. Probably the best option is to build a standalone "uberjar" which will contain all the dependencies, and will be runnable with just
java -jar myApp.jar
Please refer to this answer on StackOverflow.
you add the dependencies to the MANIFEST file
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Related
This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed last year.
Is there a way to create exe files or binaries in java? I've been only able to create java.class files after compiling the code, but I was wondering whether it is possible to create a normal program in a binary or a exe format that I could run without using command java File every time I want to run a program.
The suggested Executable jar files still require a java interpreter to be installed on the system. And as mentioned above, compiling directly into a Windows Executable looses platform independence. Yet it may be desireable to get a more native look and feel during application installation.
For this Oracle/the Java community created JPackage. It wraps your application together with the required JVM such that the whole package can be treated like a native application - regardless whether you want to run on Linux, MacOS or Windows.
You can package all the class files into a .jar file. This .jar file is executable by running java -jar <file>.jar, but most operating systems will allow you, when Java is installed, to double click on the jar file and execute it in this way.
Most build systems (such as maven and gradle) will make it easy for you to create such a file. If you have external dependencies (other jar files), you will need to create a "fat jar" that also includes those dependencies; there are plugins for those build systems to create fat jars.
You can use Launch4J to make an exe file, here you can find more info - https://youtu.be/jPKxqc8Zg-0
This question already has answers here:
Whats best way to package a Java Application with lots of dependencies?
(7 answers)
Executable Jar with depedencies
(5 answers)
Closed 7 years ago.
I made my first application with Java in Eclipse now I need to repackage or create an executable file file from that project. I used a lot of external jar libraries that I would like to export whit the project. Any solution is welcome.
I suggest to use apache maven to make a fat jar in conjunction with the launch4j maven plugin to make an self-contained exe (i.e. a wrapper for the jar).
you can make a executable jar file of that project. And then make a .bat file. From that .bat file you can invoke that executable jar.
Eclipse will pack you yourprojectname.jar file, which is executable file. So, in order to run it you may just run in command line (a.k.a cmd) java -jar path/to/your/project.jar. And of course it is more easier to run it with script. There are .bat files which are something like .bash scripts in linux/unix
Right click on your Eclipse project in ProjectExplorer
Export...
Java\Runnable jar file
On dialog Runnable JAR file export
4.1. Choose the main() method you need to start from the popup "Launch configuration"
4.2. Specify the output jar like: d:\dest.jar
4.3. "Finish'
This question already has answers here:
Including all the jars in a directory within the Java classpath
(25 answers)
Closed 7 years ago.
I am trying to use a feature from the Apache Commons called StringUtils. However this requires you download the libary and add it so I can use the code import org.apache.commons.lang3.StringUtils;. My problem is I am unsure of where to add it to so that I may compile my program in command prompt. I am also unaware of what file I should to add into the required folder.
Any help would be greatly appreciated.
You'll have to make sure that the library (usually a JAR file) is in the classpath when you compile and run your application that uses the library.
The classpath is the set of JAR files and directories that Java uses to find Java class files.
See PATH and CLASSPATH in Oracle's Java Tutorials. (This is about setting the environment variables PATH and CLASSPATH).
As an alternative to setting the CLASSPATH environment variable, you can use the -cp or -classpath options of the javac and java commands:
javac -cp C:\MyProject\lib\somelibrary.jar;. com\mypackage\MyProgram.java
java -cp C:\MyProject\lib\somelibrary.jar;. com.mypackage.MyProgram
If you are not using any IDE, then include below line in your .java file.
import org.apache.commons.lang3.StringUtils;
then we you compile the .java class from cmd prompt, include the apache commons jar in classpath. like
javac -cp ../commons**.jar your_class.java
You will need the CLASSPATH environment variable to point to it. Link.
Given the context of the question I assume you are not using maven or an IDE. If you are there are simpler ways.
This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 9 years ago.
Well I have made a few games and programs in java, and I was wondering, HOW DO YOU MAKE THIS EXE? I have been looking around google for the answer and I couldn't find any program or website that helped. So I want to make it EXE myself, manually. I have already tried JSmooth, and that didn't work.
You need to learn about creating an executable jar. Once you create that, you can simply run it by double clicking on that. Here is a tutorial to help you with that:
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to create a batch/cmd/sh script then you can put your java command in that script to invoke your main class.
A sample batch script taken from a related post(How to run java application by .bat file):
#ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar
%JAVA_HOME%\bin\java ro.my.class.MyClass
On the same lines, you can write a shell script on linux/unix.
Java now ships with a tool that can convert your application into an executable. The documentation is written for JavaFX applications. I think you should be able to use it for any java application though.
Check out this article discussing how to deploy a JavaFX Application:
http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm
Using this tool requires that you install Inno5 Setup (for creating an exe) or WiX (for creating an msi) or both if you want to create and deploy both an exe and an msi.
If this tool that ships with Java doesn't work for you there are projects such as install4j that convert your project to an exe. See http://www.ej-technologies.com/products/install4j/overview.html.
This question already has answers here:
How to add directory to classpath in an application run profile in IntelliJ IDEA?
(8 answers)
Closed 9 years ago.
In IntelliJ, I have setup a runtime configuration for a Java (console) application. I need to add the directory c:/tmp to the classpath that the app is run with. I guess I need to add -cp c:/tmp to one of the boxes in this dialog, but I can't figure out which one:
Classpath is configured in the Module Dependencies.
The classpath is a VM parameter, so it should belong in there. However, I'm unsure whether you can overwrite the classpath at all using this technique...
If you want to specify the classpath for adding additional jars, you should use libraries instead.
Also, are you sure you want to change the classpath and not the working folder? (The classpath determines where to find classes, the working folder defines the starting directory of your application.)