Cannot run .class file

Discussion in 'Mac Programming' started by 93BlackSVT, Dec 8, 2010.

  1. 93BlackSVT, Dec 8, 2010
    Last edited by a moderator: Dec 8, 2010

    93BlackSVT macrumors member

    Joined:
    Nov 2, 2010
    #1
    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?
     
  2. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
  3. 93BlackSVT thread starter macrumors member

    Joined:
    Nov 2, 2010
    #3
    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.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.
     
  5. 93BlackSVT thread starter macrumors member

    Joined:
    Nov 2, 2010
    #5
    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!
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Sounds like it's doing exactly what it should. The program's purpose is to print that, and it succeeded.

    -Lee
     
  7. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #7
    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.
     
  8. 93BlackSVT, Dec 8, 2010
    Last edited by a moderator: Dec 8, 2010

    93BlackSVT thread starter macrumors member

    Joined:
    Nov 2, 2010
    #8
    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 :)
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    What gave you that impression?
     
  10. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #10
    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.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    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.
     

Share This Page