views:

229

answers:

10

if you wanna code a desktop application in java for windows, mac and linux, will the code be the same for all of them?

and you just change the GUI so that the Windows application will be more Windows-like and so on?

how does it work without digging into details?

+3  A: 

You use generic UI libraries that render the same UI on different platforms.

Some use their own drawing/widget construction, others abstract the native UI elements.

Simple.

Examples: Swing, AWT(with bugs, read below) and QT(with caveats) or SWT (people seem to like it)

Aiden Bell
i heard that awt is deprecated. so it stands between Swing and QT?
weng
The only issue with this answer is that AWT doesn't always work with all operating systems. I did have an experience with a full screen application which didn't work on Mac but worked perfectly fine on my Windows XP machine.
indyK1ng
AWT is still available in the normal java system--Swing is built on top of it.
Bill K
AWT is still usable but has concurrency bugs and other problems that will not be resolved.
Pool
Don't forget SWT, the toolkit used in Eclipse RCP (upon which the Eclipse IDE is built). It abstracts the native widgets for several platforms.
Mike Daniels
could you use SWT with netbeans?
weng
AWT attempted to provide a thread-safe GUI and failed; but it's perfectly usable if you discipline yourself to do everything on the event dispatch thread, just as you must for Swing. And as Bill K said, Swing is built on an AWT base, as are a number of other GUI libraries (I have even done one with limited widget support for a specific project that required transparent running on J2ME handheld devices).
Software Monkey
You can use any library you want in any IDE, SWT in NetBeans included. How much extra support you get, I don't know.
ZoFreX
+2  A: 

Not only will the code be the same, but the compiled result will be the same (the output of javac, the java compiler), at least theoretically. The JVM installed on each operating system takes care of translating the compiled output (called "bytecode") into the platform-specific code. This means, for the most part, you can e.g. design and build your Java app on Linux and run it on Windows or Mac.

Matt
More details, perhaps violating the "without going into details": it is possible to write platform specific "native" code that is, of course, native to that OS. For the most part, however, if you stick to what is supplied with Java and well-designed libraries, your complied application should run across platforms.
Matt
but could you in you Application do like: "if OS = Windows, run codes...else if OS = Mac, run codes?
weng
The JVM compiles it into platform specific native code at runtime. Java tends to be nearly as fast as C (about half the speed, as opposed to 1/100th the speed for Ruby and 1/20th for most other languages), and on (very rare) occasion faster since it can compile with information not available at build time.
Bill K
@noname yes, you could, but then you're essentially de-cross-platforming it, so you have to maintain that part of your app. Still, sometimes it is not possible to do something in a totally general way, so it is a necessary evil. The nice thing about Java is that you generally have to do that _after_ your app works to make it work better, instead of _before_ your app works to make it run at all.
Matt
+5  A: 

One of Java's selling points is that is is "Write Once, Run Anywhere" (pretty much).

The compiled bytecode is executed by the Java Virtual Machine (JVM). The JVM is specific to the platform that it runs on but the same bytecode can be executed by any platform that has a JVM. The code does not have to be recompiled.

Swing is Java's cross-platform GUI toolkit. You can configure it so that it has a GUI that looks the same across different platforms (it won't look native on any of them), or you can tell it to use the most appropriate look-and-feel for the platform that it is executing on. So if you run on Windows it will look like a Windows app, if you run on OS X it will look like a native Mac app.

Dan Dyer
what do you mean with "(it won't look native on any of them)"
weng
There is a cross-platform look-and-feel, which is the default. It doesn't look like a Windows app, it doesn't look like a Mac app, it looks like a Java app. But as I mentioned, you can tell Swing to instead use the "system look-and-feel", which means the UI will be skinned, for want of a better term, to look like native apps on the platform it is running on.
Dan Dyer
he means that it will look, noticeably, like a Java app. It will look similar to the native apps, but the windows, and the dialogs (like the FileOpen box) will be quite different. Usable, but different.
Yar
Dan, the "system" look and feel does not look and feel native. It looks and feels native-ish :)
Yar
@yar On the Mac it's pretty much perfect, thanks more to Apple than Sun (you may need to tweak a few properties to get the full integration). I'll concede the point about the file dialogs, as I didn't have those in mind. The Windows native look-and-feel is very good in Java 6. I'd say it looks native, even it's only 98% rather than 100% perfect.
Dan Dyer
You're forgetting a rarely-mentioned visual advantage of Swing: It's not just a choice between "Java look and feel" or "native look and feel", you can also create your own. If you want total control over how your app looks, Swing is the option to go for: http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html
ZoFreX
Thanks Dan, I've changed tack in my answer. This is particularly good news because I'm building an app in Swing now. Good things have gotten better!
Yar
+1 for 'pretty much'
Fortega
+1  A: 

If you want your application's UI to be "windows-like" on Windows, "Mac-like" on Mac and so on, take a look at the SWT and RCP technology that underpins Eclipse. SWT in particular has the kind of properties that you want.

The downside (if you want to call it that) is that your application won't be able reuse any existing AWT / Swing based libraries. And obviously, you've got to learn how to use the SWT stack.

Stephen C
doesnt Swing provide this Platform-like interface?
weng
@noname - they claim to, but you may find that SWT does a better job. Take a look at the website I linked to.
Stephen C
if im using Netbeans i can use SWT without problems? (feels like they are coupling SWT to Eclipse on every article)
weng
@noname - I don't see why you could not use Netbeans for SWT development, though you might have difficulty testing / running SWT-based code *inside* the Netbeans JVM. However, it would undoubtedly be simpler to use Eclipse. [You could consider this as an "opportunity to broaden your skills" :-) ]
Stephen C
at the moment im learning Java with Netbeans and i find it very easy to understand. however, if i go with SWT i will use Eclipse:)
weng
+1  A: 

People used to say that Swing did not produce native results on different platforms. Apparently that's all changed, and Swing is now nigh-perfect. Go Swing! This is great news for those of us who love Java (and Groovy! and JRuby!). This may be premature, but it's in the works.

Java will work and well, and you will be able to write and compile once for the VM. However, you will need to package your app for each platform differently if you want the user to not have to type java -jar app.jar. On OSX you need to make a .app (very simple) and there are many launching options for Windows and Linux, too. Also Java WebStart is another possibility.

Hope that helps.

Edit: Getting your Swing to work on OSX and seem native takes some extra steps. See here.

Yar
so with Swing you application will not look the same as the native applications? So what GUI API would you use to accomplish that? (so thats why eclipse and netbeans dont look native=))
weng
Maybe it was true years ago that Swing apps didn't look native, but they are pretty much indistinguishable these days, particularly on Mac and Windows. On Windows, Swing now delegates to the OS to draw the components.
Dan Dyer
Thanks noname for pointing out that Eclipse (SWT) and Netbeans (Swing) do not look native. I don't know if there are any options, but as you yourself notice -- Dan Dyer seems to feel differently -- Swing and SWT do not look native. But they do look good :)
Yar
@yar. didnt you noticed that they didnt look native?=)
weng
@yar My only disagreement is your first sentence. I did vote you down because I think that's plain wrong (if you reword it I'll be happy to change my vote). Swing does "look" native on Windows and Mac. You may have a point about it not being 100% perfect, but it's pretty close.
Dan Dyer
I've changed my mind completely. Swing apps are perfectly native looking. Ish.
Yar
@yar: i like your fast-adaptable mind, perfect for the programming world:)
weng
Thanks noname, as O'Sensei said once (though I can't find the quote) when asked how he never falls: I do fall, I just get up really fast. Or something like that...
Yar
+2  A: 

The code itself will run on any platform that has a Java virtual machine, because all the JVMs translate the same exact bytecode into OS-specific machine code.

As for the look and feel, there is a method (UIManager.getSystemLookAndFeelClassName()) that allows you to make your interface use the GUI that matches the rest of the OS. It returns different results on different OSes.

Kaleb Brasee
is this Swing you are talking about?
weng
Yes - look and feel is a Swing thing.
Joshua McKinnon
+1  A: 

If you factor your application into three different layers:

  • logic
  • presentation ( if it's a graphical application this will use Java2D or equivalent )
  • interaction - buttons, menus, dialogs, etc

Then you can use the same code for all three on different platforms, but it will only 'feel' right on one of them. Typically the logic and presentation will not change, but the interaction will need to change if the Java application is to mimic native applications.

For example, the default on OS X is to have the menu at the top of the screen, and the default on Windows and Gnome is to have the menu at the top of the window. Gnome usually has application properties under edit, Windows under Windows and OS X under the apple symbol ( or something ), so the details of the menus shouldn't be the same. Each system has different layout and font rules, only some of which are implemented by Swing native mimicking look-and-feel.

Another option is to make a web application, which takes quite a bit of the variation away - you don't have windows, menus and dialogs as such buttons are provided by the browser so aren't your problem, and users don't expect platform specific behaviour from web applications.

Pete Kirkham
so one code could be deployed to all platforms AND the same code could be used for web application? how about mobile application? do i have to change/rewrite the code?
weng
The logic would be the same for web app, but you'd need different presentation. I've never targeted mobile, so don't know all the restrictions, but you would have to target the logic at what is available on J2ME rather than J2SE
Pete Kirkham
but the logic in web apps are using Java EE on top of Java SE. so that would make the desktop application not suitable for the web?
weng
Java EE does not supply any logic, only persistence and some lifecycle. You can either build your logic in a neutral manner and just call it from a servlet/jsp, ignoring most of J2EE, or use one of the other application frameworks which don't couple you strongly to J2EE. It depends on what sort of application you're creating - I create engineering tools so there's very little workflow or SQL or forms, most of the work is in the modelling, so J2EE's services are irrelevent - if a model is embedded in a servlet it might do one SQL query to get simulation parameters. YMMV.
Pete Kirkham
@noname: No, you couldn't use the same code for a GUI on all platforms and also a web app. You could use the same code for a GUI on all platforms, but a web app is quite different.
Jay
+1  A: 

In summary, yes, java is generally write once run anywhere. It's "Skinned" to fit the platform. You do have to pay attention to hardware information like screen size if you expect it to work on a phone.

Note that it also will be running on cable boxes--I'm working on some of that stuff.

Even interactions with platform specific elements are often in the same codebase--so if you have a library that detects the existence of the windows toolbar, you might detect it and support it if it's available, but the code would just not detect it on a mac (or might use a different library).

A competent engineer would not, however, have to recompile for a different platform.

Bill K
A: 

If your code interacts with the file system or invokes JNI, it won't be the same.

Amir Afghani
The code can be the same, you may have to have exception code after detecting the OS, but it will NOT require a recompile in either of these cases.
Bill K
Yes, you have to take care to make it thus.
Amir Afghani
You would need to compile for each and every platform and dynamically load whatever shared library in your Java program on the fly for the latter case - I believe thats what you meant Bill, right?
Amir Afghani
Not true for file system; true by definition for JNI - Java *Native* Interface.
Software Monkey
OK, I was lazy with my answer - but in essence what I was saying was that portability isn't free when you deal with the file system.
Amir Afghani
A: 

I'm presently working on a Web app where we routinely do our development on Windows but testing and production is on Linux. In the past I've written GUI apps where the exact same code ran on both Windows and Linux. I've never done Java on a Mac, but I presume it ports as well there as between Windows and Linux.

The beauty of Java is that 90% of the time you don't even have to think about it. You just write code that will work on the platform you're developing on and it will port magically.

The only thing that is routinely an issue for me is working with files. There are some features in Java that help, but sometimes you have to go out of your way to take advantage of them. Like, file names are different: On windows you have a drive specifier (like "C:") while on Linux you don't, and on Windows you separate directory names with backslashes while on Linux you separate them with forward slashes. There are functions to let you handle this with generic code but there are times when that's a pain. Etc.

If you want to use specific fonts, you won't find the same fonts on different platforms. But you can't rely on finding specific fonts on different boxes with the same OS. If you want to use specific fonts, you really need to check what fonts are available on the box and let the user select the one they want or have the program examine font characteristics to pick one.

I'm hard-pressed to think of anything else that's been an issue for me. I'm sure if you try you can come up with things to break on one or the other, but those are the only things I recall ever running into without looking for trouble.

Jay