13

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

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

/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

JavaLu
  • 133
  • 1
  • 1
  • 5
  • It is cross-platform (e.g. you don't have to recompile JARs), but there are still some quirks, so you have to take care. You should post a relatively small program that shows problems on Linux. – Matthew Flaschen Oct 08 '10 at 16:06
  • My experience in 1.4 was that SWING was not actually portable, but I can't speak for versions since. AWT was slightly safer... – OMG Ponies Oct 08 '10 at 16:06
  • 3
    @OMG: Swing is very portable. The problems people have (as in controls not lining up correctly) has more to do with programmers not using layouts propertly. – Reverend Gonzo Oct 08 '10 at 16:16
  • Which JRE are you using under Linux? If you've installed Oracle's JRE, are you also sure that you are actually using it and not some other JRE, which was included with your Linux distribution and by accident is found first in the path (use "java -version" to check)? – jarnbjo Oct 08 '10 at 16:21
  • how does the awt robot die? The javadoc states that you may need special privileges to run it and enable an extension to the xwindow system. This is not an error with java but a linux security measure. – josefx Oct 08 '10 at 16:32
  • my linux box just updated with oracle's JRE, my PC is problly some random one i found long ago. I will take Thorbjørn Ravn Andersen's advise and compile the program on my Linux box. – JavaLu Oct 08 '10 at 17:04
  • 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 /usr/java/jdk1.6.0_21/jre/bin/java -jar xxxxx.jar use java -version to check if your real java is running or if it s still GCJ – JavaLu Oct 09 '10 at 04:55
  • @JavaLu you should try to run "update-alternatives --config java" "update-alternatives --config javac" to set the default jvm – josefx Oct 09 '10 at 15:47

10 Answers10

12

Linux users often run java programs with gcj instead of java. Check that you use a correct executable.

Ha.
  • 3,454
  • 21
  • 24
7

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.

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.

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • I see, I am using gcj to run my Jar. I have oracle installed jre but not sure whats the command to run the jar. – JavaLu Oct 08 '10 at 18:09
  • BTW you can use `which java` to find out which java command is actually being invoked. – Grodriguez Oct 08 '10 at 18:18
  • Will openjdk work? For me it is the other way around - I write on linux so I would like to deploy on windows. – shevy Mar 31 '20 at 02:36
5

Yes. The class and jar formats are intentionally crossplatform.

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
3

Is your application dependent upon any external libraries that you also need to have available on your Linux machine?

Dave
  • 4,546
  • 2
  • 38
  • 59
  • 1
    If this was the problem, there would be ClassDefNotFoundExceptions being thrown all over the place. – Erick Robertson Oct 08 '10 at 16:29
  • True, but we don't have much to go on other than a vague "it's broken." – Dave Oct 08 '10 at 16:30
  • er, i am away from my box so i can't recall all the errors. i do remember one of them tho: java.lang.error: not implemented. sorry for not being prepared, i was expecting 5 hours before the first reply and less intense. – JavaLu Oct 08 '10 at 17:02
3

Java says: "Write once, run anywhere".

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • 1
    When it comes to Swing it's actually more like "Write once, test everywhere" – finalman Oct 08 '10 at 16:23
  • *Write once, run anywhere* is actually more true for C than for Java. Java is supposed to be **compile** *once, run anywhere that has a JVM*. – Ben Voigt Oct 08 '10 at 16:24
  • @finalman If you don't know how to use layouts well and aren't aware of cross-platform ramifications, you probably should test under a bunch of resolutions & font sizes. You do have to allow for fonts not existing on different platforms and test for that, but it's still better than any other language in this respect--at least Java HAS layout managers that can handle changing conditions because they knew allowing such reuse of code would bring new problems.... – Bill K Oct 08 '10 at 17:27
2

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.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
1

Are you using KDE? Which theme? I had an issue yesterday that the theme "Cleanlooks" has a lot of bugs rendering widgets. Try "Oxygen".

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

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.

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=6 to see if those will work.

John Munsch
  • 19,530
  • 8
  • 42
  • 72
1

On linux it might be crucial to use the Sun JDK/JRE and not the openjdk, which is an implementation of the official sun java jdk. So it could be that, though not sure at all.

toomuchcs
  • 1,344
  • 8
  • 20
  • 25
0

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.

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)

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.

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.

Bill K
  • 62,186
  • 18
  • 105
  • 157