Results 1 to 16 of 16
Like Tree4Likes
  • 1 Post By pbrockway2
  • 1 Post By Junky
  • 1 Post By pbrockway2
  • 1 Post By Tolls

Thread: Help! New to java

  1. #1
    Iaccardi is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Exclamation Help! New to java

    Hey, I just recently started following a Java beginner tutorial and I am creating a simple "Hello World" program. I saved the HelloApp.java text file onto my desktop
    When I open the command line, I type in cd desktop...I then type in javac HelloApp.java hit ENTER and then a new blank line appears (seems as if nothing happens)...however, is this supposed to create a .class file on my desktop once its compiled? I try to type in java HelloApp and I get the following error: Could not find or load main class HelloApp

    My CLASSPATH is blank
    I added C:\Program Files (x86)\Java\jdk1.7.0_01\bin\ to my PATH setting

    Thank you for taking the time to read this. HELP

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

    Default Re: Help! New to java

    It sounds like you are doing everything correctly. Could you run the following commands and copy and post the output exactly as you see it:

    Java Code:
    cd Desktop
    dir HelloApp.*
    type HelloApp.java
    javac HelloApp.java
    dir HelloApp.*
    java HelloWorld
    The dir statements are just to check that things are where you think they are. (you always have to be skeptical!) and the type statement will print the actual code. It is possible to write code that will compile but won't function as a main class.
    zeeOW likes this.

  3. #3
    Iaccardi is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help! New to java

    Alright, when i type in dir HelloApp.* I get:
    Volume in drive C is Gateway
    Volume serial number is 4639-CF24

    Directory of C:\Users\main\Desktop

    10/19/2011 144 HelloApp.java
    1 File(s) 144 bytes
    0 Dir(s) 225,036,967,936 bytes free

    When I type HelloApp.java I get:
    class ExampleProgram {
    public static void main(String[] args)}
    System.out.printIn("Hello World");
    }
    }
    I then type in java HelloApp.java and get the blank line again. After this I type dir HelloApp.* and I get the same following message as the first time in this post.

  4. #4
    KimRS is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help! New to java

    Hi i have the same problem

    this is the error i get :

    Exception in thread "main" java.lang.UnsupportedClassVersionError: hello : Unsup
    ported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: hello. Program will exit.

    i hope it's okay for me to post this here.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help! New to java

    Quote Originally Posted by Iaccardi View Post
    I then type in java HelloApp.java and get the blank line again. After this I type dir HelloApp.* and I get the same following message as the first time in this post.
    I find it hard ot believe nothing happens. You should get some sort of error. You need to compile your file first.
    Java Code:
    javac HelloApp.java
    If it successfully creates a class file: HelloApp.class, you then run it
    Java Code:
    java HelloApp
    Pay special attention to when you do and do not use file extensions.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Help! New to java

    Quote Originally Posted by KimRS View Post
    Hi i have the same problem
    No you don't!

    You have compiled your code with a higher version of Java (eg 1.6) and then are trying to run it with a lower version of Java (eg 1.4).

    Type
    javac -version
    java -version
    to see what versions you are using.
    KimRS likes this.

  7. #7
    KimRS is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help! New to java

    Quote Originally Posted by Junky View Post
    No you don't!

    You have compiled your code with a higher version of Java (eg 1.6) and then are trying to run it with a lower version of Java (eg 1.4).

    Type
    javac -version
    java -version
    to see what versions you are using.
    thank you Junky it works like a charm now ;D

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

    Default Re: Help! New to java

    Quote Originally Posted by Iaccardi View Post
    Alright, when i type in dir HelloApp.* I get:
    Volume in drive C is Gateway
    Volume serial number is 4639-CF24

    Directory of C:\Users\main\Desktop

    10/19/2011 144 HelloApp.java
    1 File(s) 144 bytes
    0 Dir(s) 225,036,967,936 bytes free

    When I type HelloApp.java I get:
    class ExampleProgram {
    public static void main(String[] args)}
    System.out.printIn("Hello World");
    }
    }
    I then type in java HelloApp.java and get the blank line again. After this I type dir HelloApp.* and I get the same following message as the first time in this post.
    You should name the class and the file the same thing. So if the file is HelloApp.java change the contents to

    Java Code:
    public class HelloApp {
        public static void main(String[] args)}
            System.out.printIn("Hello World");
        }
    }
    Junky has given you the commands that will compile and run this program. It is important to note that "java HelloApp" will attempt to run a class called HelloApp and *not* ExampleProgram. (which it knows nothing about).

  9. #9
    zeeOW is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help! New to java

    Hi guys! My HelloApp is experiencing the same symptoms as the first user here. After I've installed JDK 1.7 update 13, I went to System Properties and then to Environment Variables to insert C:\Program Files\Java\jdk1.7.0_13\bin; in the Path variable. Then, I used Notepad to type

    public class HelloApp
    {
    public static void main(String[] args)
    {
    System.out.println("Hello, World!");
    }
    }

    then I saved it as "HelloApp.java". I opened the cmd and typed cd to change into the directory of the file. Afterwards, I typed javac HelloApp.java. Nothing appears. Then, I typed java HelloApp and the same error comes up (error: Could not find or load main class HelloApp).

    When I typed dir HelloApp.*, this appears:
    Volume in drive C has no label.
    Volume Serial Number is 8ABD-1289

    Directory of C:\Users\Vaio\Documents\Programming

    12/02/2013 PM 10:01 423 HelloApp.class
    12/02/2013 PM 10:00 116 HelloApp.java
    2 File(s) 539 bytes

  10. #10
    zeeOW is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help! New to java

    (continued from above - i accidentally posted the reply above without finishing it)

    0 Dir(s) 138,179,428.352 bytes free

    When I typed HelloApp.java, a text box appears saying Windows can't open it ><bleurrgh!!!

    Please help this poor newbie....I've been trying to solve this for hours!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Help! New to java

    Do you have a CLASSPATH environment variable set up?
    If so then post the entry here.
    It's likely it needs to be deleted, but it might be from some poor software you've installed (quicktime I'm looking at you).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    zeeOW is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help! New to java

    My CLASSPATH is C:\Program Files\Java\jre6\lib\ext\QTJava.zip

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

    Default Re: Help! New to java

    Since you have a CLASSPATH variable set (and from the looks of it, required by quicktime) it is important that you *don't* use this CLASSPATH value when compiling or running your program. The reason for this is that the classpath is the place that the Java executables (java and javac etc) look for classes. Clearly the place to which CLASSPATH refers has nothing to do with your program. Hence the need to specify the classpath some other way.

    Java Code:
    javac -cp . HelloApp.java
    java -cp . HelloApp
    The dash-c-p-space-dot in those two commands means "make the classpath the current directory". By specifying it explicitly you "overrule" the CLASSPATH setting. While not needed in this case for the javac command you might as well include it as it's a sensible default any time you compile or run things from the command line.
    zeeOW likes this.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Help! New to java

    Quote Originally Posted by zeeOW View Post
    My CLASSPATH is C:\Program Files\Java\jre6\lib\ext\QTJava.zip
    I'm psychic!
    Quicktime is the bane of many Java programs.

    Follow pbrockways' instructions for declaring the classpath for an individual execution.
    zeeOW likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    zeeOW is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help! New to java

    Thanks pbrockway2 and Tolls! XD "-cp ." works! Yay! I'm deleting Quicktime too since I hardly use it.

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

    Default Re: Help! New to java

    Quote Originally Posted by Tolls View Post
    I'm psychic!
    Of course I knew that ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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