Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 23, 2011, 05:39 PM   #1
sjoue
macrumors newbie
 
Join Date: May 2011
Problem running java program with terminal

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?
sjoue is offline   0 Reply With Quote
Old May 23, 2011, 05:55 PM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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.
jiminaus is offline   0 Reply With Quote
Old May 23, 2011, 06:49 PM   #3
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
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?
sjoue is offline   0 Reply With Quote
Old May 23, 2011, 07:45 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Which Java version are you using?

Command-line:
Code:
java -version
Post the output (copy and paste it from Terminal into a post).
chown33 is offline   0 Reply With Quote
Old May 23, 2011, 08:12 PM   #5
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
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 is offline   0 Reply With Quote
Old May 23, 2011, 08:49 PM   #6
willieva
macrumors 6502
 
Join Date: Mar 2010
It ran fine on my mac. The window popped up in the upper left of my screen.
willieva is offline   0 Reply With Quote
Old May 23, 2011, 09:30 PM   #7
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
Would it have to do with some kind of settings on my computer or for the terminal?
sjoue is offline   0 Reply With Quote
Old May 24, 2011, 12:42 AM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
Post the output of these Terminal commands:
Code:
which java
printenv | sort
Also post your OS version number.


Quote:
Originally Posted by sjoue View Post
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 by chown33; May 24, 2011 at 01:12 AM.
chown33 is offline   0 Reply With Quote
Old May 24, 2011, 07:23 PM   #9
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
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?
sjoue is offline   0 Reply With Quote
Old May 24, 2011, 07:40 PM   #10
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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.
jiminaus is offline   0 Reply With Quote
Old May 24, 2011, 08:02 PM   #11
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
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 by sjoue; May 24, 2011 at 08:32 PM.
sjoue is offline   0 Reply With Quote
Old May 24, 2011, 08:36 PM   #12
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by sjoue View Post
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.
jiminaus is offline   0 Reply With Quote
Old May 24, 2011, 08:42 PM   #13
sjoue
Thread Starter
macrumors newbie
 
Join Date: May 2011
Ohh, I see. Thanks verry much for your help!
sjoue is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
running java or not flimzy Mac Basics and Help 6 Feb 28, 2014 12:59 PM
Running a java program on OS X 10.6.8 Stud1ent Mac Programming 2 Nov 10, 2013 08:27 PM
Any way to block the MAC terminal program markinmiami Mac Basics and Help 15 Feb 25, 2013 01:16 AM
Problem running a program because of Java GrantMeThePower OS X 10.8 Mountain Lion 5 Jan 16, 2013 07:24 PM
Running my C++ in Terminal... Contraversy Mac Programming 3 Nov 4, 2012 09:12 AM

Forum Jump

All times are GMT -5. The time now is 03:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC