2

Hi all,

I have about 10 jars and when I run my program I invoke the following:

java -Xmx1024m -cp a.jar;b.jar;c.jar;whatever.jar -Dfile.encoding=UTF-8 [package][class]

I want to make this as an executable jar. I thought that when I unjar (that sounds weird, but I'm using java command - not using general zip program) the jar containing [package][class], update manifest.mf and rejar it. Unfortunately, this did not work.

Is it possible for me to make it as an executable jar or I should unjar it and sum it all?

blahman
  • 1,304
  • 1
  • 12
  • 18
  • possible duplicate of [java eclipse create executable jar](http://stackoverflow.com/questions/8159046/java-eclipse-create-executable-jar) – Perception Jan 20 '12 at 02:14

2 Answers2

1

You just need this line in your manifest file

Main-Class: my.package.Main

Then run the jar command with the m flag, and give it the name of your manifest file:

jar cmf manifest main.jar *.class

Unfortunately, jars can't contain other jars. Give the jar containing your entry point a Main-Class, and then set the classpath so that the other 9 jars are accessible.

Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56
-1

Jar is a collection of files. To make a jar an executable the most important thing is that it should contain a class that has the main() method. So, you can make a jar executable only if contains a .class file that has a main method. That class will look like this.

public class AClass
{
    public static void main(String[] args)
    {
        ...
    }jar 
       ...
}

Then and only then can you make a jar executable.

If the main() method class is present. Then to make the jar executable do this.

  1. Extract the jar
  2. In the manifest file add this line Main-Class:AClass and don't forget to press enter after this line. follow this link
  3. Rejar the class files. You got the executable jar now

To execute the jar, (suppose you made a.jar executable), then run java -jar a.jar to run the jar file.

Second thing is that the command that you have posted in the question does not require those jars to be executable. When you use -cp, then the parameters (i.e. a.jar etc..)are basically libraries or in other words, when java will look for a class file to find definition of a class or a function or whatever for that matter, it will look inside these jars if what it is looking for is not part of the java standard library.

Ankit
  • 6,772
  • 11
  • 48
  • 84
  • Creating a 'fat Jar' is usually not the way to go. 1) Occasionally it will breach the licensing arrangement for the API. 2) It will always strip out any digital signatures that were included. 3) It removes much of the benefit of auto-update in JWS. -1 – Andrew Thompson Jan 20 '12 at 02:38