Problem running java program with terminal

Discussion in 'Mac Programming' started by sjoue, May 23, 2011.

  1. macrumors newbie

    I created my java program with NetBeans and it works fine. However, when I try to run it with the terminal it can print to std out but it won't display my JFrame window (the terminal just hangs). Here my sample code:

    package hello;

    import javax.swing.*;

    public class Main {

    private static void window() {
    JFrame frame = new JFrame("Hello World");
    JLabel label = new JLabel("Hello World!");

    * @param args the command line arguments
    public static void main(String[] args) {
    System.out.println("Here is my JFrame");

    What I did was:
    javac hello/*.java
    java hello/Main

    Does anyone know what's wrong?
  2. macrumors 65816


    The code as posted works fine, except for a missing semicolon after window() in main. The JFrame appeared as a small window immediately under the Apple menu.
  3. macrumors newbie

    That's strange.. it doesn't work on my computer (even after putting in the semicolon after window). Does this mean there is something wrong with my computer?
  4. macrumors 603

    Which Java version are you using?

    java -version
    Post the output (copy and paste it from Terminal into a post).
  5. macrumors newbie

    java version "1.6.0_03-p3"
    Java(TM) SE Runtime Environment (build 1.6.0_03-p3-landonf_19_aug_2008_15_52-b00)
    Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_03-p3-landonf_19_aug_2008_15_52-b00, mixed mode)
  6. macrumors 6502

    It ran fine on my mac. The window popped up in the upper left of my screen.
  7. macrumors newbie

    Would it have to do with some kind of settings on my computer or for the terminal?
  8. chown33, May 23, 2011
    Last edited: May 23, 2011

    macrumors 603

    Post the output of these Terminal commands:
    which java
    printenv | sort
    Also post your OS version number.

    That Java version indicates you've installed the SoyLatte JVM.

    The SoyLatte JVM requires the X11 graphics environment. On Mac OS X, X11 support is provided by, residing in /Applications/Utilities. I don't know if a command-line launched from Terminal will launch; you may have to start and run the command-line from the X11 xterm window.

    I recognized the landonf name from the early days of SoyLatte builds. I then googled the version string landonf_19_aug_2008_15_52 to find more info. The most useful hits came from googling soylatte java.

    SoyLatte has always been a third-party install. It's not something provided by Apple. SoyLatte has also always relied on X11 for all its GUI. So one way or another, you've installed SoyLatte on your machine, and Terminal is finding that as its 'java' command. The output from 'which java' will show where the command is located. A solution might be as simple as a different PATH environment variable. Just a guess without seeing 'which java' and output of 'printenv'.
  9. macrumors newbie

    Mac OS X 10.5.8

    which java

    printenv | sort

    I did install soylatte trying to see if that would help solve my problem..

    Also, I just tried to run xterm and nothing seems to happen; the xterm window doesn't pop up but no error messages appear (it just sorta seems to hang). Maybe this has something to do with the problem?
  10. macrumors 65816


    What happens if you delete the .class files and then compile and run with these commands?
    rm hello/*.class
    /usr/bin/javac hello/
    /usr/bin/java hello.Main
    Also it seems you might have a problem with X11. What happens if you try to manually launch X11, it's in /Applications/Utilities?

    BTW I only just noticed that you had written you'd tried to run your program with java hello/Main, that is with a slash instead of dot between hello and Main. This should have resulted in a class not found error so I'm assuming it was a typo in your post.
  11. sjoue, May 24, 2011
    Last edited: May 24, 2011

    macrumors newbie

    Ah that works!

    I think something was wrong with my X11 so I reinstalled it an it works fine now. (Maybe that had something to do with it?)

    Also, it actually works when I used either:
    /usr/bin/java hello.Main
    /usr/bin/java hello/Main
  12. macrumors 65816


    You're right. I was thinking you'd get a class not found error because there is no class Main within the default package that just happens to live in the hello directory. There's only a class Main in the hello package. But that argument is a qualified class not a path, so it looks like java is correcting the slash into a dot for you. But get into the habit of using the first version over the second. The correct way to qualify a class with a package in Java is with a dot not a slash.
  13. macrumors newbie

    Ohh, I see. Thanks verry much for your help!

Share This Page