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

93BlackSVT

macrumors member
Original poster
Nov 2, 2010
58
0
So after a few years, I've decided to get back into programming. I flipped open my book and ran the easiest thing I could find:

Code:
public class Hello
{
     public static void main(String[] args)
     {
          System.out.println("Hello World!");
     }
}
When I search for the file though (Hello.class) and try to run it, I get the following error message pop up:

The Java class file "Hello.class" could not be launched.
Check the Console for possible error messages.

Any idea what I'm missing here?
 
Last edited by a moderator:
I'm using NetBeans to design it and run/compile. In NetBeans, it says the build is successful and does not indicate any errors, however, that error is displayed when I simply double-click the .class file.
 
I'm using NetBeans to design it and run/compile. In NetBeans, it says the build is successful and does not indicate any errors, however, that error is displayed when I simply double-click the .class file.

Where are you double-clicking the .class file: NetBeans or Finder?

Does NetBeans support double-clicked .class files?

Are you following the NetBeans procedure for running the class?

The usual process for running Java programs is to put the classes in a jar file with a manifest. The manifest must designate the main class, i.e. the class with a public static main(String[]) method. Such a jar is double-clickable. Loose class files are not, AFAIK.

Loose class files can be used to run a 'java' command at the Terminal command-line, as can a jar. But I'm not aware of .class files being double-clickable.
 
Ahh, I wasn't aware of that.

Taking that into account, I went back into NetBeans and ran a "Build" which then created the "Hello World.jar" file. However, when I double-click the .jar file, nothing seems to happen at all :/

When I view the console though, it has the following:

12/8/10 12:28:59 .com.apple.JarLauncher[1455] Hello World!
 
Ahh, I wasn't aware of that.

Taking that into account, I went back into NetBeans and ran a "Build" which then created the "Hello World.jar" file. However, when I double-click the .jar file, nothing seems to happen at all :/

When I view the console though, it has the following:

12/8/10 12:28:59 .com.apple.JarLauncher[1455] Hello World!

Sounds like it's doing exactly what it should. The program's purpose is to print that, and it succeeded.

-Lee
 
Yep, remember that your simple program only prints one line of text to the console, it doesn't open up any dialogs or windows or anything you might expect from a traditional windowing application.

Perhaps the better way to execute a program like that is to go in Terminal, change to the directory containing your class file, and type the command:

java Hello

(assuming Hello.class)

You should see the output printed when the program runs.
 
I was under the impression the out.println would send the message to the screen (via pop up?).

So the only area this will appear is in the log/console?

EDIT:
Alrighty, thanks for the info!

and sorry for the newb questions :)
 
Last edited by a moderator:
I was under the impression the out.println would send the message to the screen (via pop up?).

Sadly, no. Working with a programming language is working with a very low level of the machine, usually below the graphical interface layer. A very simple program behaves the same way on a modern PC as it would on one from decades ago. (Java is a fairly new language, but the concepts are the same). Very early computers, before GUIs, would print things to the screen and accept input from the keyboard. In fact the name of the function, "print", harkens back to the early mainframe days when there was no screen at all but a typewriter console that was literally printing the output onto a roll of paper.

Sometimes that is still the point - some programs, such as many Unix tools and applications, use the terminal input/output in order to make them simple to compile and run on a variety of platforms without worrying about how fast the computer is, what kind of video graphics it is capable of (or, in the case of a server that you would access remotely, perhaps even if it has a screen at all).

To get some text to show up in a dialog box, you need to add a bit more code that accesses some of the "higher" layers that provide the GUI components, event model, etc.
 
I was under the impression the out.println would send the message to the screen (via pop up?).

If that's the kind of behavior you want, you'll have to use something else. One option is to simply work through the book you have. Jumping into the middle isn't always a good idea.

The ACM's Java Task Force (JTF) created some classes intended for education of Java beginners. You could start there:
http://jtf.acm.org/

I suggest starting with the JTF Tutorial:
http://jtf.acm.org/tutorial/index.html

I'll warn you now: if you're a complete beginner, the JTF Tutorial may not be enough to get going with NetBeans. You might have to search for other resources, such as googling NetBeans JTF.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.