Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World!");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}

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


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

Does anyone know what's wrong?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.
 

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
Which Java version are you using?

Command-line:
Code:
java -version
Post the output (copy and paste it from Terminal into a post).
 

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
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)
 

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
Would it have to do with some kind of settings on my computer or for the terminal?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
Post the output of these Terminal commands:
Code:
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.
http://landonf.bikemonkey.org/static/soylatte/

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'.
 
Last edited:

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
Mac OS X 10.5.8

which java
/Users/staceyoue/soylatte16-1.0.3/bin/java

printenv | sort
Apple_PubSub_Socket_Render=/tmp/launch-Sz0L4Z/Render
COMMAND_MODE=unix2003
DISPLAY=/tmp/launch-VNYDiE/org.x:0
HOME=/Users/staceyoue
LANG=en_CA.UTF-8
LOGNAME=staceyoue
MANPATH=/usr/share/man:/usr/local/share/man:/usr/X11/share/man:/Library/TeX/Distributions/.DefaultTeX/Contents/Man:/usr/X11/man
PATH=/Users/staceyoue/soylatte16-1.0.3/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/texbin
PWD=/Users/staceyoue
SHELL=/bin/bash
SHLVL=1
SSH_AUTH_SOCK=/tmp/launch-Y5TA5d/Listeners
TERM=xterm-color
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=240.2
TMPDIR=/var/folders/6S/6SgJQ8UlERmFVdIYdy+hTk+++TI/-Tmp-/
USER=staceyoue
_=/usr/bin/printenv
__CF_USER_TEXT_ENCODING=0x1F5:0:0


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?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
What happens if you delete the .class files and then compile and run with these commands?
Code:
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.
 

sjoue

macrumors newbie
Original poster
May 23, 2011
7
0
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
 
Last edited:

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.