How to Convert a .Java or a .Jar File into a Linux Executable File ( Without a .Jar Extension, Which Means It's Not a .Jar File )

How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )

I found a solution, which is exactly what I want after hours of searching and trying. It is to use the gcj command in linux.

It works like gcc for C and g++ for C++ which compile C or C++ programs into executables.

First install gcj by using the terminal by typing:

sudo apt-get install gcj-jdk

After that, cd into the directory of where the .jar file is located, let's say the .jar file is myFile.jar, then do:

gcj myFile.jar -o newNameofTheFile --main=theMainClassNameofTheFile

to compile the .jar file. And it should work like an executable by just running it in the command line like this:

./newNameofTheFile

How to run java application from terminal by simply calling its name?

Unix shells can't run jar files. The only solution to this are a) aliases, or b) writing a minimal wrapper around this, in a file called myapp

#!/bin/sh

java -jar myapp.jar

and putting it in your bin directory. Don't forget to make it executable (chmod 755 myapp). You can then call it by myapp.

Notice that you can be much cleverer about finding the right version etc. You might simply take such a script from your favourite MIT/BSD licensed Java application, probably, if you include its license with your software.

How to make an executable JAR file?

A jar file is simply a file containing a collection of java files. To make a jar file executable, you need to specify where the main Class is in the jar file. Example code would be as follows.

public class JarExample {

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your logic here
}
});
}
}

Compile your classes. To make a jar, you also need to create a Manifest File (MANIFEST.MF). For example,

Manifest-Version: 1.0
Main-Class: JarExample

Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder. In the command prompt, go to the folder where your files placed, and create the jar using jar command. For example (if you name your manifest file as jexample.mf)

jar cfm jarexample.jar jexample.mf *.class

It will create executable jarexample.jar.

How to copy a jar file from a folder located at myfolder/folderName to urs/lib/jvm/java-1.8/jre/lib/etc In Fedora Linux?

you want the cp command. it copies what ever you specify in the first argument to the second arguements location.
https://www.geeksforgeeks.org/cp-command-linux-examples/

sudo cp myFolder/folderName/somefile urs/lib/jvm/java-1.8/jre/lib/etc



Related Topics



Leave a reply



Submit