PDA

View Full Version : Java 2 Woes




Wes
Dec 11, 2002, 10:11 AM
I am just beginning to learn Java and I want to make sure my compilers are fixed up and function correctly. Here is some source I got to check it:

/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}

Now I put this into my compilers (Terminal, MRJAppbuilder, JJedit) and they all compile. Then they make the .class file. Then when I run it I need a main-class. What would I need to do in this situation? Sorry for my stupidity...:confused:



Wes
Dec 11, 2002, 02:33 PM
I know there are Java people here, plz reply :)

robbieduncan
Dec 11, 2002, 04:33 PM
Save the text you have above into a file called HelloWorldApp.java

In the same directory (in Terminal) type javac HelloWorldApp.java

After a couple of seconds you should be returned to a terminal prompt.

Type java HelloWorldApp (note not HelloWorldApp.java or HelloWorldApp.class) and the string Hello World! will appear in your terminal.

Wes
Dec 11, 2002, 04:47 PM
Cool thanks a lot! That works to get it in terminal, but the HelloWorldApp.class generated by that will still not open in mac os x, same error as before. :confused:

robbieduncan
Dec 11, 2002, 06:49 PM
This is a command line app. It will not open a window to display this text in or anything else.

pnz999
Dec 11, 2002, 08:47 PM
whats a good java editor for the mac? i use jcreator in my winxp.

macktheknife
Dec 11, 2002, 09:33 PM
First compile your program. Go into Terminal and go to the directory where the .java file is located. Type (no quotation marks): "javac file.java" and hit return. If the compilation succeeds, it should should have created a .class file and brought up no errors.

Next, run your program. Right after you've compiled your program, type: "java file" with no extension. The Java VM will look for the class file with the main() method and execute accordingly. The Terminal will spit out "Hello World!"

BTW, if you are trying to create a click-able Java program, you will not get it to work unless it's a Swing program (i.e. one that has a GUI).

As for a Java editor, I like Project Builder from Apple just fine.

Wes
Dec 12, 2002, 09:10 AM
Do you guys have a bit of source that has a gui so I can test that too?

pnz999
Dec 12, 2002, 09:17 AM
"As for a Java editor, I like Project Builder from Apple just fine."

I have to sign up for Apple ADC to get Project Builder?

BTW, what about a C++ editor/compiler for mac?

macktheknife
Dec 12, 2002, 10:08 AM
Originally posted by W-_-W
Do you guys have a bit of source that has a gui so I can test that too?

You can try this one. Compile it, go to MRJAppBuilder, and select the HelloJava4.class file as the main class name.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HelloJava4
{
public static void main( String[] args ) {
JFrame frame = new JFrame( "HelloJava4!" );
frame.getContentPane().add( new HelloComponent4("Hello Java!") );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setSize ( 300, 300);
frame.setVisible ( true );
}
}

class HelloComponent4 extends JComponent
implements MouseMotionListener, ActionListener, Runnable
{
String theMessage;
int messageX = 125, messageY = 95; // coordinates of the image

JButton theButton;

int colorIndex; // current index into someColors
static Color[] someColors = {
Color.black, Color.red, Color.green, Color.blue, Color.magenta };

boolean blinkState;

public HelloComponent4( String message) {
theMessage = message;
theButton = new JButton("Change Color");
setLayout( new FlowLayout() );
add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
Thread t = new Thread( this );
t.start();
}

public void paintComponent( Graphics g ) {
g.setColor(blinkState ? getBackground() : currentColor( ));
g.drawString( theMessage, messageX, messageY );
}

public void mouseDragged(MouseEvent e) {
// save the mouse coordinates and paints the image
messageX = e.getX();
messageY = e.getY();
repaint();
}

public void mouseMoved(MouseEvent e) {}

public void actionPerformed( ActionEvent e ) {
// did somebody push the button?
if (e.getSource() == theButton)
changeColor();
}

synchronized private void changeColor() {
// change the index to the next color
if (++colorIndex == someColors.length)
colorIndex = 0;
setForeground( currentColor() );
repaint();
}

synchronized private Color currentColor() {
return someColors[colorIndex];
}

public void run( ) {
try {
while(true) {
blinkState = !blinkState; // Toggle blinkState
repaint (); // show the change
Thread.sleep(1000);
}
} catch (InterruptedException ie) { }
}
}

macktheknife
Dec 12, 2002, 10:20 AM
Originally posted by pnz999
"As for a Java editor, I like Project Builder from Apple just fine."

I have to sign up for Apple ADC to get Project Builder?

BTW, what about a C++ editor/compiler for mac?

If you got the Jaguar upgrade separately (i.e. it didn't come pre-installed in your Mac), you should have a "Developer's Tools" CD. Also, check to see if it's already installed: go to your root directory (not your home directory) and see if there's a folder called "Developer." If there is, Project Builder should be located in the "Applications" folder.

BTW, here are some other Java IDEs I've found:

Netbeans (http://www.netbeans.org)
jGRASP (http://www.jgrasp.com)
Borland JBuilder Personal Edition (http://http://www.borland.com/products/downloads/download_jbuilder.html)

The first two IDEs also compiles non-Java programs like C and C++. Speaking of which, you can compile C and C++ programs in Terminal. Go to where your C and C++ source code directory (where you've stored them) in Terminal. For C files, type "gcc hello.c" (or whatever the file is called) and it should produce an a.out file. Type "./a.out" to execute. The prodcedure is the same for C++ files except you type "g++ hello.cpp" (again, whatever the C++ code is called) to compile.

Wes
Dec 12, 2002, 11:58 AM
Originally posted by macktheknife


You can try this one. Compile it, go to MRJAppBuilder, and select the HelloJava4.class file as the main class name.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HelloJava4
{
public static void main( String[] args ) {
JFrame frame = new JFrame( "HelloJava4!" );
frame.getContentPane().add( new HelloComponent4("Hello Java!") );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setSize ( 300, 300);
frame.setVisible ( true );
}
}

class HelloComponent4 extends JComponent
implements MouseMotionListener, ActionListener, Runnable
{
String theMessage;
int messageX = 125, messageY = 95; // coordinates of the image

JButton theButton;

int colorIndex; // current index into someColors
static Color[] someColors = {
Color.black, Color.red, Color.green, Color.blue, Color.magenta };

boolean blinkState;

public HelloComponent4( String message) {
theMessage = message;
theButton = new JButton("Change Color");
setLayout( new FlowLayout() );
add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
Thread t = new Thread( this );
t.start();
}

public void paintComponent( Graphics g ) {
g.setColor(blinkState ? getBackground() : currentColor( ));
g.drawString( theMessage, messageX, messageY );
}

public void mouseDragged(MouseEvent e) {
// save the mouse coordinates and paints the image
messageX = e.getX();
messageY = e.getY();
repaint();
}

public void mouseMoved(MouseEvent e) {}

public void actionPerformed( ActionEvent e ) {
// did somebody push the button?
if (e.getSource() == theButton)
changeColor();
}

synchronized private void changeColor() {
// change the index to the next color
if (++colorIndex == someColors.length)
colorIndex = 0;
setForeground( currentColor() );
repaint();
}

synchronized private Color currentColor() {
return someColors[colorIndex];
}

public void run( ) {
try {
while(true) {
blinkState = !blinkState; // Toggle blinkState
repaint (); // show the change
Thread.sleep(1000);
}
} catch (InterruptedException ie) { }
}
}


ok I have mad the .java and compiled. Now in MRJ what should I put:

Main Classname: HelloJava4
Classpath:?
Output File:?
Merged Files:?

macktheknife
Dec 12, 2002, 12:10 PM
If you go to MRJAppBuilder, you should be required to do only two things: find the main .class file in the top box and name the location where you want the app to be built. You can ignore the that big box in the center.

Wes
Dec 12, 2002, 12:29 PM
Ok, i do that, I click on the new "app" and the dock makes room for it like it is going to open, and then the dock goes back to its normal size and doesn't open.

macktheknife
Dec 12, 2002, 12:39 PM
Can you post me some screenshots showing what you did? In any case, here's a more simple test GUI program. Give this one a shot:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TopLevelDemo {
public static void main(String[] args) {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("TopLevelDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create the menu bar. Make it have a cyan background.
JMenuBar cyanMenuBar = new JMenuBar();
cyanMenuBar.setOpaque(true);
cyanMenuBar.setBackground(Color.cyan);
cyanMenuBar.setPreferredSize(new Dimension(200, 20));

//Create a yellow label to put in the content pane.
JLabel yellowLabel = new JLabel();
yellowLabel.setOpaque(true);
yellowLabel.setBackground(Color.yellow);
yellowLabel.setPreferredSize(new Dimension(200, 180));

//Set the menu bar and add the label to the content pane.
frame.setJMenuBar(cyanMenuBar);
frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

//Display the window.
frame.pack();
frame.setVisible(true);
}
}

Wes
Dec 12, 2002, 01:06 PM
Here is the pic:
The app just won't open. It gets REALLY annoying. It still just resizes the dock to let it open like a normal app then dies without a message telling me why. It really should be easier than this...

macktheknife
Dec 12, 2002, 01:17 PM
Well, assuming you got your app built ("Application successfully built."), you didn't do anything wrong using MRJAppBuilder. I tried building the HelloJava4 app just in case and it worked for me perfectly.

I don't know what happened exactly, but try restarting your computer or something. Make sure you have Java 1.3 installed (1.4.1 is available but wait for the final Mac release)--it shouldn't make a difference, but who knows?

Are you just trying to execute the program or trying to make a clickable version of the program? You can still execute the program through Terminal.

Wes
Dec 12, 2002, 01:20 PM
I'm trying to make a clickable version, thanks for your help btw. I did get the successful build message.

macktheknife
Dec 12, 2002, 01:32 PM
Originally posted by W-_-W
I'm trying to make a clickable version, thanks for your help btw. I did get the succesful build message.

No problem. Yeah, I don't know what else to say--you've more or less did the right thing, so the problem might be something with your computer. Of course, I can't really say that with absolute certainty, so perhaps ask a local Mac guru to take a look at it.

BTW, are you getting started in learning Java? Did you get a good book? I would recommend the good, but deceptively-named "Teach Yourself Java in 21 Days" by Laura Lemay. It's not really that good, but the slow tutorial pace is nice. Stay away from "Learning Java" from O'Reilly--it's a terrible book to learn Java if you've never programmed.

Wes
Dec 12, 2002, 01:34 PM
I recently installed the new 1.4.1 update, but from apple's developer site I used the terminal to switch it back to the other version. I have "Thinking In Java" by Bruce Eckel. This is starting to irritate me. I wanted to learn Java so I could just run my apps on anybody's computer. I don't want people to have to install a compiler and other things to make it work on their computer... HELP!

macktheknife
Dec 12, 2002, 01:40 PM
Originally posted by W-_-W
I recently installed the new 1.4.1 update, but from apple's developer site I used the terminal to switch it back to the other version. I have "Thinking In Java" by Bruce Eckel. This is starting to irritate me. I wanted to learn Java so I could just run my apps on anybody's computer. I don't want people to have to install a compiler and other things to make it work on their computer... HELP!

If that's the case, making it into a clickable app through MRJAppBuilder won't do it. What MRJAppBuilder does (I think) is to jar it in a way OS X will execute after being double-clicked. It executes all those commands you entered in Terminal except in the background. The clickable app won't work on a PC.

In order to run a Java program, the user must have at least the Java plug-in. I think they also need the SDK to execute it, but I'm not sure.

"Thinking in Java" is pretty good. You can download the full, free version from the author's website.

Wes
Dec 12, 2002, 01:46 PM
Originally posted by macktheknife


If that's the case, making it into a clickable app through MRJAppBuilder won't do it. What MRJAppBuilder does (I think) is to jar it in a way OS X will execute after being double-clicked. It executes all those commands you entered in Terminal except in the background. The clickable app won't work on a PC.

In order to run a Java program, the user must have at least the Java plug-in. I think they also need the SDK to execute it, but I'm not sure.

"Thinking in Java" is pretty good. You can download the full, free version from the author's website.

So there is no prospect of me having a clickable app on my mac either? How does direct connect work? That's java, and Limewire, all java and clickable apps. :mad:
Java is beginning to really annoy me. Thankfully I have a c book too.

Wasn't sun's motto, Write Once, Run Anywhere?

macktheknife
Dec 12, 2002, 02:15 PM
Originally posted by W-_-W


So there is no prospect of me having a clickable app on my mac either? How does direct connect work? That's java, and Limewire, all java and clickable apps. :mad:
Java is beginning to really annoy me. Thankfully I have a c book too.

Wasn't sun's motto, Write Once, Run Anywhere?

No, no, no, that's not what I meant. Once you get a clickable app with MRJAppBuilder, it should work on other Macs. OS X has built-in support for Java, so *any* Java app will work on OS X as long as you can execute it (either as a clickable program or through Terminal). Yes, Limewire, NetBeans, jGRASP, JBuilder, and other Java apps with GUI work as a clickable program. The HelloJava program I sent you can work on a PC, Mac, Unix, Linux, etc. machine as long as it supports Java.

Don't get too frustrated with Java: it has realized its "write once, run anywhere" promise though only imperfectly. Java programs would be really cool to use if there really were so many different platforms, but for all intents and purposes, Windows has become the standard, and making a Java program when 90% of your users run Windows certainly doesn't make good business sense. Client-side Java apps like Limewire will always run slower than their counterparts written in machine code and C++ since they're executed from within a VM.

However, the really fun stuff (in my opinion) is web-based Java technologies like servlets, JSP, and EJB that belong under the whole J2EE platform. You won't even need to worry about GUI for the most part when you're dealing with J2EE.

Wes
Dec 12, 2002, 02:39 PM
Originally posted by macktheknife


No, no, no, that's not what I meant. Once you get a clickable app with MRJAppBuilder, it should work on other Macs. OS X has built-in support for Java, so *any* Java app will work on OS X as long as you can execute it (either as a clickable program or through Terminal). Yes, Limewire, NetBeans, jGRASP, JBuilder, and other Java apps with GUI work as a clickable program. The HelloJava program I sent you can work on a PC, Mac, Unix, Linux, etc. machine as long as it supports Java.

Don't get too frustrated with Java: it has realized its "write once, run anywhere" promise though only imperfectly. Java programs would be really cool to use if there really were so many different platforms, but for all intents and purposes, Windows has become the standard, and making a Java program when 90% of your users run Windows certainly doesn't make good business sense. Client-side Java apps like Limewire will always run slower than their counterparts written in machine code and C++ since they're executed from within a VM.

However, the really fun stuff (in my opinion) is web-based Java technologies like servlets, JSP, and EJB that belong under the whole J2EE platform. You won't even need to worry about GUI for the most part when you're dealing with J2EE.

How about you make the excutable program and then email it to my_ibook@yahoo.com to see if the problem is my mac?

macktheknife
Dec 12, 2002, 02:52 PM
Originally posted by W-_-W


How about you make the excutable program and then email it to my_ibook@yahoo.com to see if the problem is my mac?

I'll send it to you later today, but try to execute the program from Terminal to make sure it works. If it does work, then your Java VM is working OK and the problem is probably MRJAppBuilder.

macktheknife
Dec 15, 2002, 01:00 AM
Did you ever get my app? If so, did it work out for you? Let me know how I can help.

Wes
Dec 15, 2002, 06:26 AM
Yeah sorry I've been busy, here is what hapened:

I receive five files:
Info.plist
JavaApplicationStub
MRJApp.properties
GenericJavaApp.icns
PkgInfo


I tried downloading them all to a folder and renaming the folder .app, that didn't work, maybe you could .sit or something like that.

macktheknife
Dec 15, 2002, 11:52 AM
That's what I had feared. I had tried attaching the file using Hotmail, my company's e-mail service, and finally my third-party e-mail address. Apparently the .app file is just a graphical representation of a .class file along with some other preference files.

Again, try executing your own HelloJava4 from Terminal. If it executes, it might have something to do with MRJAppBuilder.

Wes
Dec 15, 2002, 12:27 PM
I think they're must be something wrong with my Java launching software. I tried the HelloJava4 app again. I compiled, did the normal things. This time i tried launching the gui again using terminal, it thought for a sec. I was waiting for a cryptic error and it opened in the dock like a new app and ran. This was using the files generated by MRJ. Ideas?! I'm still :confused:

macktheknife
Dec 15, 2002, 02:17 PM
Originally posted by W-_-W
I think they're must be something wrong with my Java launching software. I tried the HelloJava4 app again. I compiled, did the normal things. This time i tried launching the gui again using terminal, it thought for a sec. I was waiting for a cryptic error and it opened in the dock like a new app and ran. This was using the files generated by MRJ. Ideas?! I'm still :confused:

You mean the files I sent you or the files you compiled yourself? Also, Java GUI programs usually take an extra second or so to launch (since it's being executed from within the Java VM).

Wes
Dec 15, 2002, 02:49 PM
Could you just post the source here, or just email me it?

macktheknife
Dec 15, 2002, 03:16 PM
Originally posted by W-_-W
Could you just post the source here, or just email me it?

The "HelloJava4" file I posted on the previous page is the one. Just copy it, paste it in a new Java file from Project Builder (or any plain text document), name it as "HelloJava4.java", and compile.

RogueLdr
Dec 15, 2002, 03:19 PM
Hey, just wondering if you have checked with the ADC site for solutions?

Here (http://developer.apple.com/qa/java/java29.html) is a link that may be of help.

Also, for a potentially more beginner-friendly, JBuilder Personal 7 from Borland can be had for the low low cost of FREE! It's a pretty good, fairly feature complete environment with good debugging tools and decent help resources, and is OS X native. It can be found here (http://www.borland.com/products/downloads/download_jbuilder.html) . You do have to register with BOrland, but it is pretty painless. Hope this helps!

RL

macktheknife
Dec 15, 2002, 03:38 PM
I'll insert some images to show the exact steps you might need to take. I'm posting them here just in case anyone has the same problem in the future.

First create a Java file from Project Builder:

http://www.spyre.net/~macktheknife/tutorial/pic1.jpeg

Then name the file "HelloJava4." It doesn't really matter what you name it, but the name of the file must match the name of the public class you give in the Java file in the line "public class NAME_HERE".

http://www.spyre.net/~macktheknife/tutorial/pic2.jpeg

Go ahead and paste the code I posted in the other page. The text in green doesn't really matter, but just make sure the first few lines up to "public class HelloJava4" match what I have.

http://www.spyre.net/~macktheknife/tutorial/pic3.jpeg

Then compile the code in Terminal. I saved it on my Desktop, so I went to my Desktop directory and typed "javac HelloJava4.java."

http://www.spyre.net/~macktheknife/tutorial/pic4.jpeg

Two files should have been created.

http://www.spyre.net/~macktheknife/tutorial/pic5.jpeg

Then go back to Terminal and type "java HelloJava4" to execute the program.

http://www.spyre.net/~macktheknife/tutorial/pic6.jpeg

Finally, a HelloJava4 program should execute and a window should pop up.

http://www.spyre.net/~macktheknife/tutorial/pic7.jpeg

Wes
Dec 15, 2002, 03:46 PM
I'm having trouble loading up any of your pictures on your tutorial you ever so nicely created for us retards.

Houston we still have a problem, I can't get double clickable apps. But can launch gui apps from the finder.

macktheknife
Dec 15, 2002, 04:20 PM
Originally posted by W-_-W
I'm having trouble loading up any of your pictures on your tutorial you ever so nicely created for us retards.

Houston we still have a problem, I can't get double clickable apps. But can launch gui apps from the finder.

Hum, if that's the case, it might be something wrong with the MRJAppBuilder. Did you install it along with the Developer's tools CD? Perhaps a reinstallation of MRJAppBuilder is in order? Don't worry too much, though: It's not the end of the world if you can't create a double-clickable Java GUI app. As long as you can execute the program from Terminal, it'll be OK. ;)

Wes
Dec 15, 2002, 04:26 PM
I wonder if the apps that I create will run on other macs. I will reinstall dev tools to see if it makes a difference. It better, it will be so cool to be able to make my own clickable apps, that will be the icing on my coffee flavoured cake. (Get it?)

Taft
Dec 15, 2002, 04:32 PM
I'm sorry, but I don't understand the confusion on this thread. Here's my rendition of why you're having problems.

Java is indeed a completely platform neutral technology. What you aer having problems with is the invocation of the java interpreter. The one hurdle that java didn't overcome (and could never, really) is that different platforms have different methods of starting up programs and of starting programs from their repective "finders."

A perfect example of this is OS X's methods for identifying executable programs from the finder. ANY GUI program in OS X has to have certain elements in its .app folder in order to be launchable as an application from the finder. There are ways around this (like creating a unix shell script to launch a pure Java program).

In addition, OS X handles java very well, but it also extends it with additions to help it integrate into the OS better. The end result is that OS X java programs aren't truely "native java" programs. This is a minor inconvenience as the differences in the code base between OS X, Unix and Windows "platform native" java programs (ie. double clickable) is negligable.

OS X's overhead for making a platform native java program is daunting the first time you try it. But once you get your head around it, its really not that bad. Look at Apple's Java documentation that comes with the developer tools. It should have examples and all of your answers.

This will show you how to make any pure java Jar file into a double clickable app in OS X.
Developer/Documentation/Java/pdf/MacOSXJava.pdf

This will show you how to create a completely Cocoa Java application:
Developer/Documentation/Cocoa/JavaTutorial/index.html

Taft

Taft
Dec 15, 2002, 04:39 PM
As for Java editors, in my opinion IntelliJ's IDEA is the best out there (www.intellij.com). It has a number of great tools to ease development (the refactoring tools are really great). It also sports full Ant integration (Ant is a build tool similar to, but easier to use and more powerful than, make).

The only thing it doesn't cover is GUI design integration. If you NEED that, try JBuilder from www.borland.com. They have a free version. Its an allright IDE, but nothing compared to IDEA.

Taft

macktheknife
Dec 15, 2002, 04:48 PM
Originally posted by W-_-W
I wonder if the apps that I create will run on other macs. I will reinstall dev tools to see if it makes a difference. It better, it will be so cool to be able to make my own clickable apps, that will be the icing on my coffee flavoured cake. (Get it?)

The HelloJava4 program you compiled will work on any platform with a Java VM. Users will have to execute it from the command console. If you want a double-clickable version of the program, however, you will need to create it separately depending on the platform.