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

Beier
  • 3,127
  • 10
  • 28
  • 24
  • 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. – Gnoupi Aug 17 '09 at 07:58
  • Thanks for the headsup, good to know. but the program is purely server side, so there is no UI involved – Beier Aug 17 '09 at 08: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. – Gnoupi Aug 17 '09 at 08: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"? – Beier Aug 17 '09 at 08: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? – Beier Aug 17 '09 at 08:37
  • Note that JAR files can reference other JAR files, but can't *contain* other JAR files. – Brian Agnew Aug 17 '09 at 10:10

7 Answers7

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
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • If I set CLASSPATH, do I still need to 'Add External JARs" in my project? – Beier Aug 17 '09 at 07:59
  • 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 Aug 17 '09 at 08:01
  • @Gnoupi - because the CLASSPATH specifies *libraries* it makes no assumptions or asserts no limits on what program will run with these. – Brian Agnew Aug 17 '09 at 08:08
  • 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) – Gnoupi Aug 17 '09 at 08:16
  • @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? – Beier Aug 17 '09 at 08:19
  • @Gnoupi - yes, you're right that the CLASSPATH can become crowded. So I set the CLASSPATH per invocation shell script (i.e. per app) or per user if the user account on that machine runs strongly related stuff that uses the same library set (not often, I confess) – Brian Agnew Aug 17 '09 at 10:01
  • @unknown - Eclipse will be isolated from your CLASSPATH setting. The above is for running only. And yes - if you have other stuff running then setting CLASSPATH may affect them and you should probably use the -cp option or set CLASSPATH in an app-specific shell-script. See my above comment to Gnoupi – Brian Agnew Aug 17 '09 at 10:08
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.

firstthumb
  • 4,627
  • 6
  • 35
  • 45
  • He can also check the ".classpath" file generated by eclipse, to see if all paths are relative. – Gnoupi Aug 17 '09 at 07:55
  • 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. – firstthumb Aug 17 '09 at 08:00
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.

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
  • 3
    To be exact, compiling does use the JVM to run the compiler (which is itself written in Java). – Michael Borgwardt Aug 17 '09 at 08:00
  • @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. – Brian Agnew Aug 17 '09 at 10:02
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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 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? – Beier Aug 18 '09 at 08:05
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>
martin clayton
  • 76,436
  • 32
  • 213
  • 198
Gnoupi
  • 4,715
  • 5
  • 34
  • 50
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.

KarlP
  • 5,149
  • 2
  • 28
  • 41
  • Thanks for the FatJar plugin tip, looks like it can save me some work – Beier Aug 17 '09 at 09:36
  • A subset of fatjar functionality has been merged in Eclipse 3.5 "Runnable jar" support. – Thorbjørn Ravn Andersen Aug 17 '09 at 11:49
  • 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? – Beier Aug 18 '09 at 06:25
-3

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

krishna
  • 1,366
  • 2
  • 15
  • 23
  • 2
    Because that's not his use-case ? Try to fix the problem, not the user. – Gnoupi Aug 17 '09 at 07:52
  • 1
    I meant to add it as a comment. But I did not have enough rep score to do that. – krishna Aug 17 '09 at 08:03
  • 1
    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 ?" – Gnoupi Aug 17 '09 at 08:05
  • 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. – krishna Aug 17 '09 at 08:08
  • 1
    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. – Anders Hansson Aug 17 '09 at 10:12