0

I have migrated from Linux (Ubuntu) to Mac OS. I have wrote an application in Java (swing). I have run it successfully the same code in windows and linux but in mac os I have some problems.

Here is the code and problems in comments:

public MainForm() {
    setResizable(false);
    setAutoRequestFocus(false); // This method is undefined for type MainForm
    initComponents();
}

Another:

Object[] industries = jList1.getSelectedValuesList().toArray(); //  This method is undefined...

And the last one:

setType(Type.UTILITY); // Type can't be resolved as variable

Of course I can't compile it.

Java versions: Ubuntu:

java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b10) Java HotSpot(TM) Server VM (build 23.3-b01, mixed mode)

MacOS:

java version "1.6.0_35" Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811) Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

I can't update java (because It seems to be the newest version for mac). I want to develop this app on mac now.

mitch
  • 2,235
  • 3
  • 27
  • 46
  • 2
    You don't need to recompile it for each OS, because Java is portable; one JAR file can be executed similarly on Windows, Linux, Mac, or any operating system on which a JVM can be run. – FThompson Sep 24 '12 at 18:41
  • @Vulcan is right, you should be able to copy the jar from Windows to the Mac and run it there. However, you might not if your Mac's version of Java is older than the version of Java you use for your code on the other platforms. Run `java -version` on the Mac and edit your question to display the output. – Brian Sep 24 '12 at 18:43
  • Actually, you can install Java 7 on the Mac. Apple is no longer distributing it, but are providing the look & feel libraries to Oracl. Go to [Downloads](http://www.oracle.com/technetwork/java/javase/downloads/jdk7u7-downloads-1836413.html), the 5th option down (under the Linux distros) you'll find the Mac version – MadProgrammer Sep 24 '12 at 20:02

2 Answers2

2

After doing some digging for the methods giving you problems, it's clear you're not running the same JDK on every platform. Specifically, your Windows and Linux boxes are running JDK 1.7, and your Mac OS X box is running JDK 1.6 or older. See this question for using JDK 1.7 on Mac.

The fact remains that you don't need to compile your application on every platform. Java is a "compile once, run everywhere" language. The bytecode produced by the compiler works for every platform, regardless of which platform compiled it, so long as you didn't introduce any system dependencies in the code yourself.

In other words, Java itself is platform-agnostic as long as your code is platform-agnostic. Your problem is just a JDK version error, so upgrading your Mac's version of the JDK to 1.7 will fix this.

Note that you can't run binaries compiled with a 1.7 source target in Java 6 or lower. You can change the source target to 1.6 on compile time, but this will preclude you from using the Java 7 API (such as the getSelectedValuesList method).

Community
  • 1
  • 1
Brian
  • 17,079
  • 6
  • 43
  • 66
  • Even if the program is complied on Windows or Linux, the fact that it was compiled under Java 7 will prevent it from running under Java 6. I know you can target the binaries to Java 6 compatible, but if your using APIs from Java 7 it still won't run. Am I mistaken? The better solution would be to upgrade the JDK on the Mac to Java 7 – MadProgrammer Sep 24 '12 at 19:59
  • @MadProgrammer Isn't that what I said? I'll clarify my answer to be more clear. – Brian Sep 24 '12 at 21:19
  • @Brain - Missed the last paragraph, my apologies. – MadProgrammer Sep 24 '12 at 22:40
  • @MadProgrammer Oh no, you're not at fault, I added that last paragraph onto my answer after your comment. You're safe :3 – Brian Sep 24 '12 at 22:45
  • Thank you very much I have installed OpenJDK 1.7 and set in eclipse new java and now works. Thank you. – mitch Sep 25 '12 at 12:37
1

Create executable jar on Linux, and execute it on Mac. It should works well.
If you want develop your app on another OS, then check that JDK has same version.
Full version should be equals. 1.6_31 hould be equals too

Ilya
  • 29,135
  • 19
  • 110
  • 158