使用GCJ编译时如何将jar包与*.java文件链接在一起?

发布于 2024-12-20 23:30:22 字数 158 浏览 3 评论 0原文

我有以下文件:

A.jar(包含 *.class 文件)
B.jar(包含*.class文件)
Program.java(包含具有主要功能的Program类,依赖于A.jar和B.jar)

如何使用GCJ构建可执行文件Program

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-12-27 23:30:22

我已经有一段时间没有接触 Java 了,所以下面的内容大部分都是我的想法。

在linux中,通常java程序是由包装脚本启动的。对于您的情况,此包装器脚本可以是 Program,内容:

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

如果您希望只有一个 jar 文件,那么您可以“unjar”A.jar 和 B.jar 并创建一个新的 jar,假设 Program.jar 包含 A.jar、B.jar 和您的 Program.class 中的所有类,然后您创建一个小清单文件,告诉您执行 jar 文件时要运行哪个类(在本例中是您的程序.类)。

清单文件的内容(我们称之为manifest.txt):

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

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

请注意“Main-Class: Program”行后面的空行 - 这是必需的。

因此,创建单个 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 ..

现在创建 shell 脚本包装器 Program

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

使其可执行:

chmod +x Program

并运行它:

./Program

如果有效就鼓掌,否则扔烂番茄!

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!

调妓 2024-12-27 23:30:22

这对我有用:

gcj -c A.jar -o Ao
gcj -c B.jar -o Bo
gcj --main=Program --classpath=A.jar:B.jar -o Program 敖博 Program.java

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文