Results 1 to 16 of 16
  1. #1
    duff18 is offline Member
    Join Date
    Feb 2011
    Location
    Cagliari, Sardinia, Italy
    Posts
    4
    Rep Power
    0

    Default Absolute beginner

    Hi everyone,
    I'm trying to run a very simple program on Mac OS X 10.6.6 from the terminal.
    This is the code:


    Java Code:
    	public class Welcome1
    {
    	
    	public static void main( String args[] )
    {
    	System.out.println( "Welcome to Java Programming!" );
    
    	} 
    }
    I saved on Welcome1.java, typed

    javac Welcome1.java

    then

    java Welcome1.java

    but got this error:


    Exception in thread "main" java.lang.NoClassDefFoundError: Welcome1/java
    Caused by: java.lang.ClassNotFoundException: Welcome1.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    To run a program, you don't run the .java file. You probably want something like this:

    javac Welcome1.java (this creates a .class file)
    java Welcome1 (this runs the .class file)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    duff18 is offline Member
    Join Date
    Feb 2011
    Location
    Cagliari, Sardinia, Italy
    Posts
    4
    Rep Power
    0

    Default

    Oh, thank you !

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by duff18 View Post
    Oh, thank you !
    One more thing (it's not relevant to your problem); don't use the indentation and curly brackets format as you do now; it is very confusing.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    duff18 is offline Member
    Join Date
    Feb 2011
    Location
    Cagliari, Sardinia, Italy
    Posts
    4
    Rep Power
    0

    Default

    Ok,
    I'm learning from "Java How to Program"
    hence following the style of this book

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by duff18 View Post
    Ok,
    I'm learning from "Java How to Program"
    hence following the style of this book
    It's ok, I don't consider it a sin but that book (I haven't read it) uses a very funny curly bracket position style then. Real programmers and real men (like me!) use the following style:

    Java Code:
    public class Welcome1 {
    	
    	public static void main( String args[] ) {
    
    		System.out.println( "Welcome to Java Programming!" );
    	} 
    }
    It's the style used by Kernighan and Ritchie, the inventors of C. Later those sissies who came from Pascal used this abonimation of curly backet placement:

    Java Code:
    public class Welcome1 
    {
    	public static void main( String args[] ) 
    	{
    
    		System.out.println( "Welcome to Java Programming!" );
    	} 
    }
    which is still frowned up on by the real programmers and the real men (like me!) but is still used by those toothless misfits that don't know any better ;-)

    kind regards,

    Jos (<--- starter of religeous wars)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    Later those sissies who came from Pascal used this abonimation of curly backet placement:

    ....

    which is still frowned up on by the real programmers and the real men (like me!) but is still used by those toothless misfits that don't know any better ;-)

    kind regards,

    Jos (<--- starter of religeous wars)
    Prepare to be defenestrated onto an angry mob with sharp pointy sticks.

    Kind regards,

    Pete

  9. #9
    Hallowed is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Man I dunno... my book uses them like that..
    public class Welcome1
    {
    public static void main( String args[] )
    {

    System.out.println( "Welcome to Java Programming!" );
    }
    }


    and my teachers say FAILURE if I do it any other way. o.O

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Hallowed View Post
    Man I dunno... my book uses them like that..
    Java Code:
    public class Welcome1 
    {
    	public static void main( String args[] ) 
    	{
    
    		System.out.println( "Welcome to Java Programming!" );
    	} 
    }

    and my teachers say FAILURE if I do it any other way. o.O
    The ultimate rule is: your teacher's / boss's / review board's / judge's rules rule. Period.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Prepare to be defenestrated onto an angry mob with sharp pointy sticks.
    By whom? Those weak Pascalians who put their curcly brackets at the most preposterous positions consistently all over their code? They deserve to have their teeth and molars removed with a baseball bat (if they had any). Pfah!

    kind regards,

    Jos (<--- loves sensible, scientific discussions ;-)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    UrgeToLearn is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    duff18,

    I would like to add few more things to this exception.

    When you write a code, you will write it in java and save the file in .java format. When you compile this .java file, by using the command javac Welcome1.java, after the compilation this file would be converted to .class file and it doesnt remain as .java. So when you run your code, JVM considers your .class file which is already compiled and ready for execution. So its not necessary for you to type the .java extension of the file name.

    javac Welcome1.java //Compilation

    java Welcome1 //Execution of the class file.

    Hope this helps you to understand why we should not append .java extension during execution.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    The ultimate rule is: your teacher's / boss's / review board's / judge's rules rule. Period.
    For what it's worth: if you're using Eclipse goto Window>Preferences>Java>Code Style>Formatter and define a new formatter named "sissy whimpy teacher" and set the braces formatting to be on the 'next line' optionally indented. Keep the default formatter rule for the real men (like me!) if they want their code formatted by Eclipse (Cntrl-A, Cntrl-Shift-F)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    duff18 is offline Member
    Join Date
    Feb 2011
    Location
    Cagliari, Sardinia, Italy
    Posts
    4
    Rep Power
    0

    Default

    I love this discussion about curly brackets.
    it concerns also phylosophical issues :)
    anyway thanks for your help

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    So its not necessary for you to type the .java extension of the file name.

    It's not so much that it is not necessary, but rather that the java and javac expect different kinds of arguments. javac expects the name of a source file (like "Welcome1.java") while java expects the name of a class (like "Welcome1").

    Given a command like

    Java Code:
    java Welcome1.java

    the java executable will interpret "Welcome1.java" as a class called "java" in a package called "Welcome1". If you read the error message it is saying that no such class exists.

    ----------------------

    As regards the more important matter of curly brace placement, K&R would actually write the opening brace of function declarations on a new line rather than the correct placement as in Jos' example. In light of that, it's amazing the language had any impact whatsoever... Still they were lucky and others corrected things.
    Last edited by pbrockway2; 02-19-2011 at 08:14 AM.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    As regards the more important matter of curly brace placement, K&R would actually write the opening brace of function declarations on a new line rather than the correct placement as in Jos' example. In light of that, it's amazing the language had any impact whatsoever... Still they were lucky and others corrected things.
    I emphasized part of your reply for reasons of clarity ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. This absolute uri (http://displaytag.sf.net) cannot be resolved in either web.xml or
    By santosh.dhulipala in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-21-2010, 08:20 AM
  2. Replies: 1
    Last Post: 04-23-2010, 09:27 PM
  3. Replies: 2
    Last Post: 02-08-2010, 12:03 AM
  4. Beginner needs help!
    By Polyy in forum New To Java
    Replies: 1
    Last Post: 11-27-2008, 05:12 AM
  5. Specifying absolute path in web.xml
    By Felissa in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2007, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •