Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Windows vs Mac problems

Started by Megaman_22 Jan 9, 2007 at 9:45 PM 7 replies 1.3k views
Original Post
Megaman_22
Megaman_22
Hi. I've been working on this project, kind of between two laptops, my school one, which is a powerbook, ( damn you Angus King ), and my real one, which is a HP. I use JCreator on the HP, and XCode on the Apple. Anyway, I'v been shuffling this project back on forth between the computers, and I find that on the Apple, I have to hardcode in the paths for my images and fonts & such, whereas on my windows box I can use getClassLoader().getResource()( I think that's it ) to access them, and then it doesn't matter where I have my project directory. I have a feeling this has something to do with how XCode always bundles your .class files and other stuff into one executable, but I have also found that if I poke around through the command prompt and make a self-executing JAR file, it won't run on the damned Mac. Worst of all, I have no program on the machine capable of running a .class file! And packages seem to be a definate no-no with XCode. Aarghhh! Probably I just don't know how to set the damn program up, but I've messed with it for weeks, and I can't get it to work. Also, while I'm ripping Apple, I'd like to say that their documentation sucks, to the point that if you follow their tutorials, you cannot get a working program, which is a major reason I switched from C++ to Java in the first place.
Eric Richards
ukdeveloper
ukdeveloper
Quote:
Worst of all, I have no program on the machine capable of running a .class file!


Quote:
If you have the Java SDK installed on the Mac then you should be able to run the .class file, just like how you can run a .class file on Windows through the command prompt. I could be very wrong however, because I have had very little experience with Macs in general. >.<



If you're on a Mac, open the Terminal and type
java
. If you get a command not found error, you've got a problem. If you get a massive list of flags and a message telling you you've got invalid input, then you're in business.

Use cd to move to the directory you've got the .class file in. Let's say the .class file is called Hello.class.

Type
java Hello
-> note you don't include the .class file extension here.
Megaman_22
Megaman_22
Okay, I've gotten the Terminal to start my program, when I moved this class file that ended with $1, into my JAR file. I had thought that was some kind of temporary file, but maybe it has something to do with threads?

I still cannot get my resources to load properly, although I've got the directories set up exactly the same as on my Windows machine.
Eric Richards
CaptainJester
CaptainJester
Those class files with $1, $2, etc are created as anonymous classes. And yes they are necessary. You also have to package all your resources into the jar file. Also I would recommend against getClassloader().getResource() and user getClass().getResource(). I have always had problems with getClassloader().

Since you are doing this on 2 different OSes, why don't you use Eclipse. It is available for all major platforms. Then you will have the same environment no matter where you work.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
pinacolada
pinacolada
Even better than using Eclipse would be to start building with Ant. Ant is basically like the c++ "make" tool except with better syntax. It's totally cross-platform and is pretty ubiquitous in the Java world (I think Mac installs it automatically when you install Xcode). And it doesn't lock you into a certain IDE; I think most IDEs know how to build straight from an Ant file.
Aldacron
Aldacron
Quote:
Original post by pinacolada
Even better than using Eclipse would be to start building with Ant. Ant is basically like the c++ "make" tool except with better syntax. It's totally cross-platform and is pretty ubiquitous in the Java world (I think Mac installs it automatically when you install Xcode). And it doesn't lock you into a certain IDE; I think most IDEs know how to build straight from an Ant file.


Eclipse doesn't 'lock you in' either. It can export Ant build files for your projects so that they may be built anywhere. I certainly do not find building with Ant to be 'better than using Eclipse' during development. Far from it. I would bite my arm off before editing Java code in an editor or IDE that doesn't support the features I regularly use in Eclipse.
Megaman_22
Megaman_22
I'm pretty sure Eclipse wouldn't help me out any, because the mac is a school laptop, and so, while I have more priviledges on it than most, because I do programming on it, it's still locked down so hard that it's little more than a web browser and a word-processor. Maybe I could get the source code, and build it, but I have a feeling that could be complicated, and I was hoping I could just figure out how to get it to work with the programs I've already got. Especially since JCreator Pro, where I do most of my work, is pretty decent.

I started using getClassLoader() because I figured hardcoding in paths through the hard disk was not good, and I happened to see a tutorial that showed it that way. Could somebody possibly post a little code to show me the proper way of going about it?
Eric Richards
Aldacron
Aldacron
Quote:
Original post by Megaman_22
I started using getClassLoader() because I figured hardcoding in paths through the hard disk was not good, and I happened to see a tutorial that showed it that way. Could somebody possibly post a little code to show me the proper way of going about it?


Take your pick:

getClass().getClassLoader().getResource("my.res");MyClass.class.getClassLoader().getResource("my.res");Thread.currentThread().getContextClassLoader().getResource("my.res");


I tend to work with the getResourceAsStream() versions, but I've never had a problem with either method. Just be aware that if you are loading from jar files, the jars need to be on the class path. Otherwise, files should be relative to the working directory.

Loading resources this way is a good thing, because it gives you the flexibility of moving resources between jars and the file system. That's important if you decide to make your app WebStartable or repackage it as an Applet. It also makes it quite easy to implement a system where users can, for example, replace a jarred resource by dropping the new version in the file system.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.