I have created a jar file abc.jar. I want my jar to be converted to .exe file using the native-image command of graalvm. The jar needs some dependencies(external jars) to work.
So my command looks like:-
native-image -jar abc.jar output -cp "abc.jar;C:\lib\*"
The exe is generated and works on my machine. Nevertheless, when I ship it to a different matching and try to run the same it doesn't work unless I place all libs under C:\lib folder(the very same folder where I placed the external dependent jars). How can I create an executable with the jars included as part of exe file.
I tried the following specified in the documentation and no luck.
Regards.
Related
I created project in netbeans. In Netbeans it compiles. But i need to know the way of compiling the project in Linux. It simply not just a file. I have folder which contains Build,nbproject,src and etc.
Please tell me the way of compiling a project created in netbeans in linux terminal.
In the most basic case you compile all .java files in the src folder. This generates .class files. You then execute the main class file (which contains a main method). Don't forget to also put all other dependent classes on the classpath.
Compile:
$ cd <yourproject>/src
$ javac $(find . -name "*.java")
Run:
java -cp ./ yourpackagename.YourMainClass
the -cp argument specifies the classpath. All the classes on this path will be considered as dependecies. In this case we put the classpath to ./ which is the current directory (src). That means if MainClass uses other classes in your project, they will be linked (made usable). If you do not specify a classpath your whole application would need to be contained in your MainClass, which is basically never the case.
Another way - much more common - you create a jar archive (basically a zip file) which contains your compiled classes. I'm pretty sure you can generate jar files within NetBeans. That generated jar file can then be executed:
$ java -jar myjarfile.jar
Most people do not directly use the IDE in order to generate the jar but rather use build tools such as Gradle or Maven.
Another option is using the jar command:
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Java creating .jar file
Hope this helps.
How can i export a jar file in eclipse with libraries? Executable jar wont work for me so that is out of the question. There is no option to do that when i try to export it..
If the jar is not running try to run it using a CLI for example command prompt java -jar <name>.jar and see the errors.
When you export the .jar there are 3 options .
One and two produces the .jar with the libraries inside.
Third option is producing a .jar (executable) with the libraries added
a different folder[ which has the same name as the jar file and it
must be named like it].
Mention that:
You may have serious problems and your executable will not run if your application has duplicate entries. So most of the time the third bullet is the best solution.
Sample image:
If you are trying to create a .jar to add it as a library to other projects then you should follow the below:
I just created a JAR file that contains external dependencies in it so that I would just have one JAR file. I did this by using FatJar. When I did this my Jar worked fine, and I was able to run it with no problems. Then I used ikvm to convert my Jar to an exe. When I try to run the exe I get this error:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate
com.simontuffs.onejar.Boot in the java.class.path: consider using
-Done-jar.jar.path to specify the one-jar filename
So the first thing I thought of was to check if there was a Manifest file that specified the classpath. There was indeed a Manifest file and this was it's contents:
Manifest-Version: 1.0 Created-By: Fat Jar/One-JAR Eclipse Plug-In
Main-Class: com.simontuffs.onejar.Boot
I'm assuming this is correct because I'm able to run the JAR fine, and this was also created by FatJar.
I'm not really sure what to do next, and I'm not sure what -Done-jar.jar.path is. Anyone have a clue of what the problem may be?
I think the issue is that the ikvmc compiler isn't seeing the JAR files embedded within the single executable JAR file you are creating.
You probably want to treat the creation of a single executable JAR file and the creation of a single Windows exe as separate processes. The Fat Jar plugin will create an executable JAR for you with no problems.
To create a Windows exe you'll probably need a batch file or ant script which invokes ikvmc on your code, and all of the JAR dependencies. I think you can provide a list of JAR files to convert on the ikvmc command line, and tell it to produce a single exe as its output. This will probably give you what you want!
i want to create a jar file of my java web application.
This web application has so many jar files related to this. so all am including in the build path. And when the time of jar file creation from eclipse,even if i had checked the checkbox for exporting the class files and resources option, the generated jar file doesn't have those library files. So when am trying to execute the jar file from command line option by using
java -jar myJarfile.jar
it will throw error like NoClassDefFoundError , can anyone help me to solve the issue.?
How can i generate the jar with all dependencies.?
You shouldn't really be including external dependencies in your jar. Just bundle up your code into your jar and specify the dependencies at runtime.
java -jar myJarfile.jar -cp someDependency.jar
I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.
But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]
It can generate jar. But the jar can only be executed in the directory where it is produced.
If I put it into another directory or machine, it complains NoClassDefFoundError.
So, my question is that how I can generate executable jar using command line as Eclipse.
A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to
1) Create a manifest file which lists all the dependency jars
Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
dependency4.jar dependency5.jar
2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.
3) In this same folder where you created your jar, place all the dependency jars.
Now your folder will look like this,
yourjar.jar (With the manifest file you created above)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar
4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.
Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :
so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.
My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post