Class Loading on Different Opperating Systems

R

Rationem

In an attempt to handle the special CMD+Q on a Mac that resides in the
application menu I had to put in the following code:

if (System.getProperty("os.name").toLowerCase().indexOf("mac")
!= -1)
{
com.apple.eawt.Application app = new
com.apple.eawt.Application();

app.addApplicationListener(new
com.apple.eawt.ApplicationAdapter() {
public void handleQuit(com.apple.eawt.ApplicationEvent
e)
{
attemptToQuit();
}
});
}

This works properly on a Mac but does not work on a windows machine
because the com.apple.eawt.* classes are not avabilible. Is there any
way I can use this code and still maintain the cross-platform aspect of
java?

Thanks,
Adam
 
C

Chris Smith

Rationem said:
In an attempt to handle the special CMD+Q on a Mac that resides in the
application menu I had to put in the following code:

if (System.getProperty("os.name").toLowerCase().indexOf("mac")
!= -1)
{
com.apple.eawt.Application app = new
com.apple.eawt.Application();

app.addApplicationListener(new
com.apple.eawt.ApplicationAdapter() {
public void handleQuit(com.apple.eawt.ApplicationEvent
e)
{
attemptToQuit();
}
});
}

This works properly on a Mac but does not work on a windows machine
because the com.apple.eawt.* classes are not avabilible. Is there any
way I can use this code and still maintain the cross-platform aspect of
java?

Sure; reflection can do what you want. The naive way would look like
this (omitting exception handling).

if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
Class appc = Class.forName("com.apple.eawt.Application");
Object app = appc.newInstance();

Class lc = Class.forName(
"com.apple.eawt.ApplicationListener");
Object listener = Proxy.newProxyInstance(
getClassLoader(), new Class[] { lc },
new InvocationHandler() {
public Object invoke(
Object proxy,
Method method,
Object[] args)
{
if (method.getName().equals("handleQuit")
{
attemptToQuit();
}
}
});

Method m = appc.getMethod("addApplicationListener", lc);
m.invoke(app, listener);
}

Of course, that's the naive way. A better way would be to put all that
code into a class that implements some interface, and then do something
like this:

if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
AppCloseHandler h = Class.forName(
"mypackage.MacintoshCloseHandler").newInstance();
h.setupClose();
}

AppCloseHandler is an interface that you write. Inside of the class
mypackage.MacintoshCloseHandler, you can make static use of the Mac-
specific classes, since this code will only get loaded when those
classes are available.

I still find it hard to believe that WindowListener.windowClosing
doesn't work properly on the Mac... but if you've tried it, I guess I
can't argue since I don't have a Mac to try it with.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Steve W. Jackson

Rationem said:
In an attempt to handle the special CMD+Q on a Mac that resides in the
application menu I had to put in the following code:

if (System.getProperty("os.name").toLowerCase().indexOf("mac")
!= -1)
{
com.apple.eawt.Application app = new
com.apple.eawt.Application();

app.addApplicationListener(new
com.apple.eawt.ApplicationAdapter() {
public void handleQuit(com.apple.eawt.ApplicationEvent
e)
{
attemptToQuit();
}
});
}

This works properly on a Mac but does not work on a windows machine
because the com.apple.eawt.* classes are not avabilible. Is there any
way I can use this code and still maintain the cross-platform aspect of
java?

Thanks,
Adam

I can't imagine why you're using toLowerCase, since the value of that
property when running on Mac OS X will be *exactly* "Mac OS X" on every
system.

In what way is it failing on Windows? Do you have code references
elsewhere (not shown here) to those Apple classes that are evaluated at
runtime on a Windows machine?

You might find more help from Apple via some useful web pages at
<http://developer.apple.com/referencelibrary/Java/idxPorting-date.html>.
On that page is a link entitled OSXAdapter that shows (with source code)
how to use reflection to hook these Apple classes into an application.
Just below it is one entitled Unsolicited About Boxes which shows more
on how to properly override the About and Quit behaviors. And just
below that is a link entitled AppleJavaExtensions where you can get stub
classes for compiling on other platforms. I would think that one or
more of those would provide useful answers.

= Steve =
 
S

Steve W. Jackson

[ snip ]
I still find it hard to believe that WindowListener.windowClosing
doesn't work properly on the Mac... but if you've tried it, I guess I
can't argue since I don't have a Mac to try it with.

WindowListener.windowClosing works exactly the same way on a Mac as it
does in Windows and Linux.

The origins of this question are not about handling window closing
events, but about the proper way when running on a Mac to handle the
system-provided Quit and About menu items. If you took a Swing app you
designed on Windows or Linux and moved it to a Mac, you would find those
items present whether you wanted them or not. And the Quit menu item,
since your app wouldn't provide any handling for it, would simply
unceremoniously terminate your application without your intervention.
The About item would display a simplistic dialog with "Java" in it
rather than using your already-built code.

= Steve =
 
R

Rationem

The code below when run on a Mac works fine. It does not work on a
windows computer citing a class not found.
 
R

Roedy Green

com.apple.eawt.Application

is this something you invented or Apple did?

If you, you have no business using Apple's package name.
If Apple, that package appears to be Apple only. At the very least
you would have to arrange for the jar to be present.
 
S

Steve W. Jackson

Roedy Green said:
is this something you invented or Apple did?

If you, you have no business using Apple's package name.
If Apple, that package appears to be Apple only. At the very least
you would have to arrange for the jar to be present.

I have a much better idea. Refer back to my earlier posts on the
subject. Apple has public posted the information on how to do this
right, so that you have access to the stub classes for compiling on any
platform and making sure the Mac-specific code only executes on a Mac.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top