简体   繁体   中英

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

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.

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.

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

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.

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

So on Windows, your CLASSPATH looks like:

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

and on Unix/Linux it will look like:

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 CLASSPATH on the command line eg

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

You should use Eclipse for 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.

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.

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.

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".

Strongly recommended, if applicable.

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

The JAR file contains a manifest file, 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.

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

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 fileset which includes all your external jars.

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

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

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.)

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM