3

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

6
  • Extra note, be careful to not use some "Windows specific" libraries, when doing so (even if it's not likely to happen). For example if you would make you own look&feel, this is a place where some classes are platform-dependent. Commented Aug 17, 2009 at 7:58
  • Thanks for the headsup, good to know. but the program is purely server side, so there is no UI involved Commented Aug 17, 2009 at 8:02
  • Interesting point, helping answers : how do you want your code to be executed ? Will it be a command line on the main class, or do you bundle all your compiled code in a jar ? Because the classpath issue is slightly different then. Commented Aug 17, 2009 at 8:23
  • Based on some tutorials I read, probably I'll bundle the sources into java and execute java -jar myprogram through linux command line. So what do you mean by "classpath issue is slightly different then"? Commented Aug 17, 2009 at 8:30
  • Maybe I get it now. How about this, on Windows dev environment, I use Eclipse to include whatever external libraries I need, compile, run, test... But for production in linux, all I need to do is to include all my internal code plus external libraries into a JAR file, and execute the JAR file on linux? Commented Aug 17, 2009 at 8:37

7 Answers 7

8

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 e.g.

java -cp /home/user/lib/abc.jar:/home/user/lib/def.jar
Sign up to request clarification or add additional context in comments.

7 Comments

If I set CLASSPATH, do I still need to 'Add External JARs" in my project?
Environment variable ? You mean the local one, which is included in the meta information from the jar file, I guess ? Because such change on the the system variable would imply that only one java program can run with such specific classpath.
@Gnoupi - because the CLASSPATH specifies libraries it makes no assumptions or asserts no limits on what program will run with these.
But it's not really the cleanest way of doing that, then, easy to end up with a crowded classpath variable. (+1 for the addition of the -cp parameter)
@Brian, If I add set the CLASSPATH, do I still need to Add External JARs in Eclipse? since the library has already been referenced to CLASSPATH. Also there will be other java programs running on the same server, so will chaing CLASSPATH affect other programes that I have no control?
|
7

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.

2 Comments

He can also check the ".classpath" file generated by eclipse, to see if all paths are relative.
Right click on module in Eclipe, in Java Build Path on the right side, select source tab, add here relative folder and put your jars in this folder.
3

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.

2 Comments

To be exact, compiling does use the JVM to run the compiler (which is itself written in Java).
@Michael - yes, except that's an implementation detail. e.g. the old Jikes compiler was native C, IIRC. I don't know what the situation is now, and using Sun's javac does use the JVM.
2

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.

1 Comment

I tried using 'Runnable Jar", it does generate a JAR nicely. But actually the .classpath in Elipse still points to my local directly, not relative. How do I change the path from absolute to relative? by hand modifying .classpath file?
1

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.

3 Comments

Thanks for the FatJar plugin tip, looks like it can save me some work
A subset of fatjar functionality has been merged in Eclipse 3.5 "Runnable jar" support.
I tried using 'Runnable Jar", it does generate a JAR nicely. But actually the .classpath in Elipse still points to my local directly, not relative. How do I change the path from absolute to relative? by hand modifying .classpath file?
1

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>

Comments

-3

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

5 Comments

Because that's not his use-case ? Try to fix the problem, not the user.
I meant to add it as a comment. But I did not have enough rep score to do that.
Comment or answer, the problem is same. It's like answering to someone asking for help with linux : "Too much trouble, why not using windows ?"
well,it's not the same.bcos I did not suggest a different IDE. If you are using Eclipse for your development, it's also available on linux.
There might be several reasons why one would like to cross-build for Eclipse. You could have a build-server build for a multitude of platforms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.