View Full Version : javac and java commands from Terminal

Jan 26, 2007, 08:19 AM
Ok, starting to code a bit here on the mac. OS X 10.4.8 on a new Intel MacPro. Java -version is:

java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-113)
Java HotSpot(TM) Client VM (build 1.5.0_06-68, mixed mode, sharing)

After I write my code (using Textwrangler) I open Terminal, and type:

javac /Users/toddburch/Documents/myjava/MyClass.java

and it works fine. Then, I type

java MyClass /Users/toddburch/Documents/myjava

and I get the "Hello World" written to stdout. So far, so good.

With my longer test program that throws up a couple dialog boxes, I do the same things:

javac /Users/toddburch/Documents/myjava/MessageDialogDemo.java

and it works fine too, other than giving me a warning for a deprecacted function. This time, though, I get additional .class files. Instead of just getting:

MessageDialog.class and
MessageDialogDemo.class, I also get
MessageDialog$1.class and
MessageDialogDemo$1.class. What's up with these?

Then, again from the root directory (/), as before, I enter

java MessageDialogDemo /Users/toddburch/Documents/myjava

and I ALWAYS get:

Exception in thread "main" java.lang.NoClassDefFoundError: MessageDialogDemo

If I "cd Documents/myjava" and enter

java MessageDialogDemo

It works fine! I'm pulling my hair out here. Why is an exception occuring when I provide the path from the command line with this second program and not the first? Could it be a classpath issue, where MessageDialog.class is not being found? If so, how to fix?

hmmmmm... thinking... trying another test

OK, I tried the -verbose option, and that didn't shed any light on this. I then tried providing a classpath:

java -cp</Users/toddburch/Documents/myjava> MessageDialogDemo /Users/toddburch/Documents/myjava

This time, bash returned, with no error message, and no program output. What could this be? Are there considerations with running a GUI from within a shell command line?

I can post the code if that would be helpful - it's an example from a Java book I typed up.

Sorry for the long post - but that's where I'm at. Todd

Jan 26, 2007, 08:57 AM
The $ .class files normally get generated if you are using inner classes (do you define a class inside another class definition).

The other problem sounds like a CLASSPATH issue.

Jan 26, 2007, 09:13 AM
I guess I do. Here's the code:

import java.awt.* ;
import java.awt.event.* ;

public class MessageDialogDemo extends Frame
implements ActionListener
Button b ;

public static void main(String args[])
MessageDialogDemo mdd = new MessageDialogDemo() ;
mdd.setVisible(true) ;
mdd.setSize(200,100) ;

super("Message Dialog Demo");

// set layout manager
setLayout(new FlowLayout()) ;

//Create a button
b = new Button("Message Dialog") ;
b.addActionListener(this) ;
add(b) ;

// Anonymous inner class handles window events
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent we)
System.exit(0) ;
}) ;

public void actionPerformed(ActionEvent ae)
String message = "This is a message." ;
MessageDialog md = new MessageDialog(this, "Message Dialog", true, message);
md.show() ;

class MessageDialog extends Dialog
implements ActionListener
Button ok ;

MessageDialog(Frame parent, String title, boolean mode, String message)
super(parent, title, mode) ;

//create and add "Center" panel
Panel pc = new Panel() ;
Label label = new Label(message) ;
pc.add(label) ;
add(pc,"Center") ;

// create and add a "South" panel
Panel ps = new Panel() ;
ok = new Button("OK") ;
ok.addActionListener(this) ;
ps.add(ok) ;
add(ps,"South") ;

//Lay out components and set the intial size of this dialog box
pack() ;

//Anonymous inner class handles window events
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent we)
System.exit(0) ;
}) ;

public Insets getInsets()
return new Insets(40,20,20,20) ;

public void actionPerformed(ActionEvent ae)
dispose() ;

Jan 26, 2007, 09:17 AM
OK, I figured it out. It was a syntax issue with the -cp spec. This works:

java -cp /Users/toddburch/Documents/myjava MessageDialogDemo /Users/toddburch/Documents/myjava

I had used the < and > before - not needed.


EDIT: This works too, and is shorter:

java -cp /Users/toddburch/Documents/myjava MessageDialogDemo