Java 如何在 unix 上运行 .jar 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run .jar file on unix?
提问by musicking123
I have generated .jar file in windows.
I have to execute that .jar file in unix .
我在 Windows 中生成了 .jar 文件。
我必须在 unix 中执行那个 .jar 文件。
I am running that command (java -jar myJar.jar
), but it's giving
我正在运行该命令 ( java -jar myJar.jar
),但它给出了
java.lang.UnsupportedVersionError
I am using java version 1.5.0.12 in Unix.
我在 Unix 中使用 java 版本 1.5.0.12。
采纳答案by VonC
java.lang.UnsupportedVersionError
You must be trying to launch a jar compiled with JDK6, with a local java1.5.
您必须尝试使用本地 java1.5 启动使用 JDK6 编译的 jar。
You can either:
您可以:
- upgrade your jvm on Unix to 1.6
- or re-compile your classes with a
- 将 Unix 上的 jvm 升级到 1.6
- 或重新编译您的类
:
:
javac -source 1.5 -target 1.5 -bootclasspath /path/to/jre1.5/lib/rt.jar
to check if you can generate 1.5 bytecode compatible.
检查您是否可以生成兼容的 1.5 字节码。
回答by Bombe
Same as under Windows.
与 Windows 下相同。
java -jar file.jar
回答by Vanya
- Check that JDK or JRE is installed. Install if it is not.
- set JAVA_HOME= # JDK/JRE home directory
- $JAVA_HOME/bin/java -jar file.jar
- 检查是否安装了 JDK 或 JRE。如果不是,请安装。
- set JAVA_HOME= # JDK/JRE 主目录
- $JAVA_HOME/bin/java -jar file.jar
You should also keep in mind that JVM are not forward compatible, so if you've made a jar-file using JDK 1.6, it won't work with JDK 1.5!
您还应该记住 JVM 不向前兼容,因此如果您使用 JDK 1.6 创建了一个 jar 文件,它将不适用于 JDK 1.5!
In this case you may either:
在这种情况下,您可以:
install JDK 1.6 to Unix
recompile the jar with
-target 1.5
flag (but you may get any sort of errors due to API incompatibilities, so the first way is much better)
将 JDK 1.6 安装到 Unix
使用
-target 1.5
标志重新编译 jar (但由于 API 不兼容,您可能会遇到任何类型的错误,因此第一种方法要好得多)
回答by izb
Have you tried using a newer version of Java on your Unix system?
您是否尝试过在您的 Unix 系统上使用较新版本的 Java?
If you control the jar file, could you target Java 1.5 when it's compiled?
如果您控制 jar 文件,您能否在编译时以 Java 1.5 为目标?
回答by Jesper
UnsupportedVersionError
means that you have compiled the Java source code with a newer version of Java than what you're trying to run it with. Java is downwards compatible (newer versions of Java can run Java programs compiled with older versions), but not upwards compatible (older versions of Java cannot run Java programs compiled with newer versions).
UnsupportedVersionError
意味着您已经使用比您试图运行它的 Java 版本更新的 Java 版本编译了 Java 源代码。Java 向下兼容(新版本的 Java 可以运行用旧版本编译的 Java 程序),但不向上兼容(旧版本的 Java 不能运行用新版本编译的 Java 程序)。
You say you're using Java 5 on Unix. Did you compile it using Java 6 on Windows? Then it's obviously not going to work.
你说你在 Unix 上使用 Java 5。您是否在 Windows 上使用 Java 6 编译它?那么它显然是行不通的。
Possible solutions:
可能的解决方案:
- Compile your source code using Java 5 on Windows (the safest option)
- Use the compiler flags:
-source 1.5 -target 1.5
while you compile (this will not protect you against using Java 6-only classes from the standard library, so this is not a full guarantee that your program will run without errors on Java 5) - Upgrade to Java 6 on Unix
- 在 Windows 上使用 Java 5 编译源代码(最安全的选项)
- 使用编译器标志:
-source 1.5 -target 1.5
编译时(这不会保护您不使用标准库中的仅限 Java 6 的类,因此这不能完全保证您的程序在 Java 5 上运行时不会出错) - 在 Unix 上升级到 Java 6
回答by Thorbj?rn Ravn Andersen
Recompile your application with Java 5 on your Windows machine.
在您的 Windows 机器上使用 Java 5 重新编译您的应用程序。
(Java 6 generates Java 6 compatible byte code by default to use new facilities. The easiest for you is to install a Java 5 JDK and use it to recompile your application)
(默认情况下,Java 6 生成 Java 6 兼容字节码以使用新工具。对您来说最简单的是安装 Java 5 JDK 并使用它重新编译您的应用程序)
回答by Harshita Sethi
I faced the same problem. This is because the runtime and compile time JDK versions are different. Make the jar through eclipse after changing the java compiler version. The following link helped me.
我遇到了同样的问题。这是因为运行时和编译时 JDK 版本不同。更改java编译器版本后通过eclipse制作jar。以下链接帮助了我。