Andrei Sedoi
Andrei Sedoi

Reputation: 1544

How do I link jar packages together with *.java files during compilation using GCJ?

I have the following files:

A.jar (containing *.class files)
B.jar (containing *.class files)
Program.java (containing Program class with main function, which depends on A.jar and B.jar)

How can I build an executable file Program using GCJ?

Upvotes: 1

Views: 1881

Answers (2)

Andrei Sedoi
Andrei Sedoi

Reputation: 1544

This works for me:

gcj -c A.jar -o A.o
gcj -c B.jar -o B.o
gcj --main=Program --classpath=A.jar:B.jar -o Program A.o B.o Program.java

Upvotes: 0

holygeek
holygeek

Reputation: 16215

It's been a while since I played around with Java so the following are mostly off the top of my head.

In linux usually a java program is launched by a wrapper script. For your case this wrapper script can be the Program, the content:

#!/bin/sh
java -cp A.jar:B.jar:/path/to/dir/where/Program.class/is/in Program

If you prefer to have only a single jar file then you can "unjar" A.jar and B.jar and create a new jar, say Program.jar that contain all the classes from A.jar, B.jar and your Program.class, and you create a little manifest file that tells which class is to be run when you execute the jar file (in this case it's your Program.class).

The content of the manifest file (let's call it manifest.txt):

-----8<------
Main-Class: Program

----->8------

Note the blank line after the "Main-Class: Program" line - it's needed.

So the create the single Program.jar:

gcj --classpath A.jar:B.jar Program.java
mkdir tmp
cd tmp
jar xf ../A.jar
jar xf ../B.jar
cp ../Program.class .
jar cmf ../manifest.txt ../Program.jar .
cd ..

Now create the shell script wrapper Program:

#!/bin/sh
java -jar /path/to/Program.jar

Make it executable:

chmod +x Program

and run it:

./Program

Applause if it works, throw rotten tomatoes otherwise!

Upvotes: 1

Related Questions