Java 2 Woes

Discussion in 'Mac Help/Tips' started by Wes, Dec 11, 2002.

  1. Wes macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #1
    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:
     
  2. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    It Works!

    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.
     
  4. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #4
    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:
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    This is a command line app. It will not open a window to display this text in or anything else.
     
  6. pnz999 macrumors member

    Joined:
    Nov 6, 2002
    #6
    whats a good java editor for the mac? i use jcreator in my winxp.
     
  7. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #7
    Here is what you do

    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.
     
  8. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #8
    Do you guys have a bit of source that has a gui so I can test that too?
     
  9. pnz999 macrumors member

    Joined:
    Nov 6, 2002
    #9
    "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?
     
  10. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #10
    You can try this one. Compile it, go to MRJAppBuilder, and select the HelloJava4.class file as the main class name.

    Code:
    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) { }
        }
    }
    
     
  11. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #11
    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
    jGRASP
    Borland JBuilder Personal Edition

    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.
     
  12. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #12
    ok I have mad the .java and compiled. Now in MRJ what should I put:

    Main Classname: HelloJava4
    Classpath:?
    Output File:?
    Merged Files:?
     
  13. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #13
    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.
     
  14. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #14
    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.
     
  15. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #15
    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:

    Code:
    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);
        }
    }
    
     
  16. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #16
    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...
     

    Attached Files:

  17. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #17
    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.
     
  18. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #18
    I'm trying to make a clickable version, thanks for your help btw. I did get the successful build message.
     
  19. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #19
    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.
     
  20. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #20
    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!
     
  21. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #21
    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.
     
  22. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #22
    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?
     
  23. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #23
    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.
     
  24. Wes thread starter macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #24
    How about you make the excutable program and then email it to my_ibook@yahoo.com to see if the problem is my mac?
     
  25. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #25
    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.
     

Share This Page