在 Windows 中编译的 Java 可以在 Linux 中运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3892282/
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
Will Java compiled in windows work in Linux?
提问by JavaLu
My Java program is in working order when i use it under Windows(Eclipse and Bluej). I compress it to a Jar and send it to my red hat and bang. nothing works. It breaks on the weirdest things, such as text field set text will not show, JPasswordfield just disappeared, Java AWT ROBOT dies too... the list goes on, first i thought it must be my Linux JRE is out of date, but i installed latest JRE then the JDK with no improvement at all. I have a feeling that i miss understood the Java cross plat ability. I also tried to remove all of my functions and guts to see what is breaking but it seems every second thing is breaking, other than the some of the major GUI components and most of the back end stuff. basically any thing that uses some thing fancy will blowup in my face, such as making a text field in to a password field...
当我在 Windows(Eclipse 和 Bluej)下使用它时,我的 Java 程序处于工作状态。我把它压缩到一个罐子里,然后把它送到我的红帽子里,然后砰的一声。没有任何效果。它打破了最奇怪的事情,例如文本字段设置的文本不会显示,JPasswordfield 刚刚消失,Java AWT ROBOT 也死了......名单还在继续,首先我认为它一定是我的 Linux JRE 已经过时了,但我安装了最新的 JRE,然后安装了 JDK,但没有任何改进。我有一种感觉,我想念了解 Java 跨平台能力。我还尝试删除我的所有功能和胆量,看看是什么出问题了,但似乎每一件事都在出问题,除了一些主要的 GUI 组件和大多数后端内容。基本上任何使用一些奇特的东西都会在我面前爆炸,例如将文本字段放入密码字段......
This is my first time posting ;) please be nice to the newbie!
这是我第一次发帖 ;) 请对新手好一点!
Thanks!!!
谢谢!!!
SOLVED!!! Yay. Problem solved!!! It was because my Java path isn't set, so my GCC/GCJ jumped in instead of my oracle java, even tho i used java -jar xxx.jar. so I put in the java directory path from of my java -jar xxx.jar and worked like a charm. unless you set the path, you have have to do this manually
解决了!!!好极了。问题解决了!!!这是因为我的 Java 路径没有设置,所以我的 GCC/GCJ 代替了我的 oracle java,即使我使用了 java -jar xxx.jar。所以我把 java -jar xxx.jar 中的 java 目录路径放入其中,并且工作起来很有魅力。除非您设置路径,否则您必须手动执行此操作
/usr/java/jdk1.6.0_21/jre/bin/java -jar xxxxx.jar
/usr/java/jdk1.6.0_21/jre/bin/java -jar xxxxx.jar
java -version to check if your real java is running or if it s still GCJ
java -version 检查您的真实 java 是否正在运行或者它是否仍然是 GCJ
采纳答案by Ha.
Linux users often run java programs with gcj instead of java. Check that you use a correct executable.
Linux 用户经常使用 gcj 而不是 java 来运行 java 程序。检查您是否使用了正确的可执行文件。
回答by Thorbj?rn Ravn Andersen
Yes. The class and jar formats are intentionally crossplatform.
是的。类和 jar 格式是有意跨平台的。
Are you absolutely certain that your jar file was copied correctly to your Linux machine? If you used ftp, it is crucial to use binary instead of ascii transfer.
您绝对确定您的 jar 文件已正确复制到您的 Linux 机器上吗?如果您使用 ftp,那么使用二进制而不是 ascii 传输是至关重要的。
回答by Dave
Is your application dependent upon any external libraries that you also need to have available on your Linux machine?
您的应用程序是否依赖于您还需要在 Linux 机器上可用的任何外部库?
回答by Aaron Digulla
Are you using KDE? Which theme? I had an issue yesterday that the theme "Cleanlooks" has a lot of bugs rendering widgets. Try "Oxygen".
你在使用 KDE 吗?哪个主题?昨天我遇到了一个问题,主题“Cleanlooks”有很多渲染小部件的错误。试试“氧气”。
If you can, try the code on a different Linux box (maybe it has a hardware defect). Run "memtest" (should be an option in the boot loader or on your rescue CD).
如果可以,请在不同的 Linux 机器上尝试代码(可能有硬件缺陷)。运行“memtest”(应该是引导加载程序或应急 CD 上的一个选项)。
回答by 卢声远 Shengyuan Lu
Java says: "Write once, run anywhere".
Java 说:“一次编写,随处运行”。
回答by Qwerky
Swing should be fully portable. Try using the metal look and feel, which is widely supported. I have my doubts about java.awt.Robot
though and wouldn't be suprised if this was the source of your problems.
Swing 应该是完全便携的。尝试使用广泛支持的金属外观和感觉。我对此表示怀疑,java.awt.Robot
如果这是您问题的根源,我也不会感到惊讶。
回答by John Munsch
I have a Swing app which I distributed via Java Web Start that I haven't updated since 2004 which I know still works fine on Mac OS X, Windows, and Linux. I ran it just the other day because somebody emailed me a question about it. It was originally written on Windows but has never shown any problems on other operating systems or even in much much later versions of Java.
我有一个 Swing 应用程序,我通过 Java Web Start 分发它,它自 2004 年以来一直没有更新,我知道它在 Mac OS X、Windows 和 Linux 上仍然可以正常工作。前几天我运行了它,因为有人通过电子邮件向我发送了有关它的问题。它最初是在 Windows 上编写的,但从未在其他操作系统上出现过任何问题,甚至在 Java 的更高版本中也没有出现过任何问题。
The JVM presents a very level playing field across a wide variety of platforms and the Java API is well implemented to work the same and offer the same functionality. My bet is that you will find this problem is something entirely unrelated to your Java code. In addition to the suggestion to try your app on another machine running the same OS, be sure to try other people's Java apps running on the one which is giving problems. Try my own HotSheet: http://www.johnmunsch.com/projects/HotSheet/or a game like Legerdemain: http://roguelikefiction.com/?page_id=6to see if those will work.
JVM 在各种平台上提供了一个非常公平的竞争环境,并且 Java API 得到了很好的实现,以相同的方式工作并提供相同的功能。我敢打赌,您会发现这个问题与您的 Java 代码完全无关。除了建议在另一台运行相同操作系统的机器上尝试您的应用程序之外,请务必尝试在出现问题的机器上运行其他人的 Java 应用程序。试试我自己的 HotSheet:http: //www.johnmunsch.com/projects/HotSheet/或像 Legerdemain 这样的游戏:http://roguelikefiction.com/?page_id=6看看这些是否有效。
回答by Grodriguez
Check that you are using Sun's (Oracle's) JRE on your Linux machine and not some 3rd party Java implementation such as gcj or the likes.
检查您是否在 Linux 机器上使用 Sun 的(Oracle 的)JRE,而不是一些 3rd 方 Java 实现,例如 gcj 或类似的。
In my experience, Java applications are quite portable as long as you stick to the same JVM/runtime vendor on every target SO, but Java implementations from different vendors might behave different from each other.
根据我的经验,只要您在每个目标 SO 上坚持使用相同的 JVM/运行时供应商,Java 应用程序就非常可移植,但是来自不同供应商的 Java 实现可能彼此不同。
回答by Bill K
Java is very transportable. Chances are your VM is different. Although the Open Source VMs are getting very good, I recommend trying with Sun's VM--I've never had a problem with it running any code.
Java 非常易于移植。很有可能你的 VM 是不同的。尽管开源虚拟机变得非常好,但我建议尝试使用 Sun 的虚拟机——我在运行任何代码时从未遇到过问题。
Also, if you are using any libraries with machine language components, those will have to be different (I've noticed this with some graphics libraries that call through to the platform's 3d subsystem)
此外,如果您使用任何带有机器语言组件的库,这些库必须有所不同(我已经注意到一些调用平台 3d 子系统的图形库)
But Java itself WILL port easily to any system that has the same libraries and a good VM Very Well--I do it all the time and have been doing so for ten years and literally have never had a problem. This is running client/server systems where the clients and server were on different platforms, PC/Mac & Unix and even embedded systems--stuff just ports.
但是 Java 本身将很容易移植到任何具有相同库和良好 VM 的系统。这是运行客户端/服务器系统,其中客户端和服务器位于不同的平台上,PC/Mac 和 Unix 甚至嵌入式系统 - 只是端口。
Now, applets running in different browsers, or visually trying to match fonts on machines that don't contain the same font--Nothing will help you there.
现在,在不同浏览器中运行的小程序,或者在视觉上尝试匹配不包含相同字体的机器上的字体——没有什么能帮助你。
回答by toomuchcs
On linux it might be crucial to use the Sun JDK/JRE and notthe openjdk, which is an implementationof the official sun java jdk. So it could be that, though not sure at all.
在Linux上可能是使用Sun JDK / JRE和关键不是OpenJDK的,这是一个实现了官方的Sun Java JDK的。所以可能是这样,尽管完全不确定。