View Full Version : Problem running java program with terminal

May 23, 2011, 05:39 PM
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?

May 23, 2011, 05:55 PM
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.

May 23, 2011, 06:49 PM
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?

May 23, 2011, 07:45 PM
Which Java version are you using?

java -version

Post the output (copy and paste it from Terminal into a post).

May 23, 2011, 08:12 PM
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)

May 23, 2011, 08:49 PM
It ran fine on my mac. The window popped up in the upper left of my screen.

May 23, 2011, 09:30 PM
Would it have to do with some kind of settings on my computer or for the terminal?

May 24, 2011, 12:42 AM
Post the output of these Terminal commands:
which java
printenv | sort

Also post your OS version number.

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)

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 X11.app, residing in /Applications/Utilities. I don't know if a command-line launched from Terminal will launch X11.app; you may have to start X11.app 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'.

May 24, 2011, 07:23 PM
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?

May 24, 2011, 07:40 PM
What happens if you delete the .class files and then compile and run with these commands?

rm hello/*.class
/usr/bin/javac hello/Main.java
/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.

May 24, 2011, 08:02 PM
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

May 24, 2011, 08:36 PM
Also, it actually works when I used either:
/usr/bin/java hello.Main
/usr/bin/java hello/Main

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.

May 24, 2011, 08:42 PM
Ohh, I see. Thanks verry much for your help!