如何在 Windows 中编写 Java 代码并在 Linux 中编译和运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1286620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:57:44  来源:igfitidea点击:

How do I write Java code in Windows and compile and run in Linux

java

提问by Beier

I'm a new Java developer and would like to find out if it's possible to write Java code in Windows using Eclipse, and the code is actually compiled and run on Linux. I need to write java program for Linux (Ubuntu) environment, but I'm used to Windows for development.

我是一名新的 Java 开发人员,想知道是否可以使用 Eclipse 在 Windows 中编写 Java 代码,并且该代码实际上是在 Linux 上编译和运行的。我需要为Linux(Ubuntu)环境编写java程序,但我已经习惯了Windows进行开发。

I'm thinking of using Samba for file sharing, so I can use Windows to access the source code that resides in Ubuntu. But when I compile the code, it actually use Windows JVM, instead of Ubuntu JVM, will that matter? Also when I need to include external JAR libraries that resides in Ubuntu, the path is actually something like E:\home\java\project\abc.jar, where E: drive is a mapped network drive from Samba. so when I ran the program on Ubuntu, there won't be any D: drive anymore.

我正在考虑使用 Samba 进行文件共享,因此我可以使用 Windows 访问驻留在 Ubuntu 中的源代码。但是当我编译代码时,它实际上使用的是 Windows JVM,而不是 Ubuntu JVM,这有关系吗?此外,当我需要包含驻留在 Ubuntu 中的外部 JAR 库时,路径实际上类似于 E:\home\java\project\abc.jar,其中 E:drive 是来自 Samba 的映射网络驱动器。因此,当我在 Ubuntu 上运行该程序时,将不再有任何 D: 驱动器。

So I'm a bit confused and wonder if this is possible at all. any help will be really appreciated

所以我有点困惑,想知道这是否可能。任何帮助将不胜感激

回答by Brian Agnew

Because Java is platform-independent, it won't matter where you compile vs. where you run. So you can compile under Windows and run on Linux with no problem.

因为 Java 是独立于平台的,所以编译与运行的位置无关紧要。所以你可以在 Windows 下编译,在 Linux 上运行也没有问题。

You'll need copies of your libraries on the Linux box as wellas the Windows box. It doesn't matter where these are, but they need to be referenced via the environment variable CLASSPATH.

您将需要在 Linux 机器Windows 机器上的库副本。它们在哪里并不重要,但它们需要通过环境变量引用CLASSPATH

So on Windows, your CLASSPATHlooks like:

所以在 Windows 上,你CLASSPATH看起来像:

CLASSPATH=d:\jars\abc.jar;d:\jars\def.jar

and on Unix/Linux it will look like:

在 Unix/Linux 上,它看起来像:

CLASSPATH=/home/user/lib/abc.jar:/home/user/lib/def.jar

(note the change between colon and semicolon).

(注意冒号和分号之间的变化)。

Given the above, running

鉴于上述,运行

java MyApp.jar

will work on both platforms. Setting the CLASSPATH globally may affect different Java instances, so you may want to set it in a shell-script invoking each program. Or you can specify the CLASSPATHon the command line e.g.

将在两个平台上工作。全局设置 CLASSPATH 可能会影响不同的 Java 实例,因此您可能希望在调用每个程序的 shell 脚本中设置它。或者您可以CLASSPATH在命令行上指定,例如

java -cp /home/user/lib/abc.jar:/home/user/lib/def.jar

回答by firstthumb

You should use Eclipsefor Development IDE and add dependent jars as relative not full directory link. You can compile on Windows and run on Linux. It does not matter.

您应该使用Eclipse开发 IDE 并将依赖的 jar 添加为相对而非完整的目录链接。您可以在 Windows 上编译并在 Linux 上运行。没关系。

回答by Thorbj?rn Ravn Andersen

If you use Eclipse 3.5 please note that there is a new "Export -> Runnable jar" option, which is a subset of the FatJar plugin allowing you to both export as a single jar, and as a traditional executable jar with all dependent jars nicely put in a subdirectory.

如果您使用 Eclipse 3.5,请注意有一个新的“Export -> Runnable jar”选项,它是 FatJar 插件的一个子集,允许您导出为单个 jar,也可以导出为具有所有相关 jar 的传统可执行 jar放在一个子目录中。

We have completely switched to the latter model as it conforms nicely with our ant scripts while being completely devoid of classloader tricks, and other nastyness. Just plain old "java -jar foo/bar.jar".

我们已经完全切换到后一种模型,因为它很好地符合我们的 ant 脚本,同时完全没有类加载器技巧和其他讨厌的东西。只是普通的“java -jar foo/bar.jar”。

Strongly recommended, if applicable.

如果适用,强烈推荐。

回答by Tom Jefferys

When compiling java, it doesn't use the JVM. It simply compiles the java source to bytecode which should be machine independent. You can then run the compiled java bytecode on any machine that can run the JVM. So you shouldn't have any problems from the java perspective.

编译java时,它不使用JVM。它只是将 java 源代码编译为应该独立于机器的字节码。然后,您可以在任何可以运行 JVM 的机器上运行编译后的 java 字节码。因此,从 Java 的角度来看,您不应该有任何问题。

回答by Gnoupi

If, like suggested, you will bundle compiled source in a JAR, then you will take care about the issue at this place.

如果像建议的那样,您将编译后的源代码捆绑在 JAR 中,那么您将在此处处理该问题。

The JAR file contains a manifestfile, which describes the content, the firm, etc. It also contains the classpath, with the path to the external jars you will need. These paths will be relative, for your issue, and won't include the full D:\ something.

JAR 文件包含一个清单文件,该文件描述内容、公司等。它还包含类路径,以及您将需要的外部 jar 的路径。对于您的问题,这些路径将是相对的,并且不会包含完整的 D:\ 内容。

If you choose to use Ant to generate your JAR, the "jar" task is the place to define your manifestfile.

如果您选择使用 Ant 生成 JAR,则“ jar”任务是定义清单文件的地方。

Example :

例子 :

<manifest>
    <attribute name="Specification-Title" value="Blablabla description"/>
    <attribute name="Class-Path" value="${classpath}"/>
</manifest>

In this example, the "classpath" variable can be a filesetwhich includes all your external jars.

在这个例子中,“classpath”变量可以是一个包含所有外部 jar的文件集

<path id="classpath">
    <fileset dir="${project.libdir}" includes="*.jar" />
</path>

回答by KarlP

I think it's a bad idea to share the source code using samba (or any shared directory.)

我认为使用 samba(或任何共享目录)共享源代码是个坏主意。

Use a proper version control system for the files. If you are already not using one, you REALLY should. (Suggestion: SVN using the plugin subclipse.)

对文件使用适当的版本控制系统。如果您还没有使用它,那么您真的应该这样做。(建议:SVN 使用插件 subclipse。)

Check out the projects to your local workspace on Your Windows machine using eclipse.

使用 eclipse 将项目签出到您的 Windows 机器上的本地工作区。

Remeber that filenames are case sensitive on Linux. Try to use relative filenames.

请记住,文件名在 Linux 上区分大小写。尝试使用相对文件名。

If you are the only windows developer on a project, you might be up to more troble than it's worth, but if you are a half-decent developer you will learn Ubuntu in a couple of days anyway.

如果您是项目中唯一的 Windows 开发人员,那么您可能会遇到更多麻烦,但如果您是一个不合格的开发人员,无论如何您将在几天内学习 Ubuntu。

However, If you are doing your own application, you can compile the code on Windows, package it using for instance FatJar pluginand just execute it on the linux machine.

但是,如果您正在开发自己的应用程序,则可以在 Windows 上编译代码,使用FatJar 插件等将其打包,然后在 linux 机器上执行。

回答by krishna

Too much trouble. why not use eclipse on ubuntu as well?

太多麻烦了。为什么不在 ubuntu 上使用 eclipse 呢?