Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Main Class not loading

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Main Class not loading

    Hey guys, literally just started trying to learn Java and already running into trouble-
    Version Info-
    C:\Program Files (x86)\Java\jre7\bin>java -version
    java version "1.7.0_21"
    Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
    Java HotSpot(TM) Client VM (build 23.21-b01, mixed mode, sharing)

    Just trying a little 'Hello World' and I keep getting this error: "Error: Could not find or load main class C:\Java\hello2.java"

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


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Main Class not loading

    A class like Hello that is designed to be "run" with a main() is normally made public. Ie, is declared as "public class Hello { etc".

    More importantly it must in a file Hello.java. The file name and class name must match: this is part of Java's way of identifying classes at runtime.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Main Class not loading

    Still the same:

    C:\Program Files (x86)\Java\jre7\bin>java C:\Java\Hello.java
    Error: Could not find or load main class C:\Java\Hello.java

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

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Main Class not loading

    You have to first compile, then run. The compiler executable is javac.exe: it turns your .java file into a binary class file. Then you use the java.exe executable to actually run the file.

    The commands are

    javac -cp . Hello.java
    java -cp . Hello

    Notice how javac expects the name of a file (to compile) while java expects the name of a class (with no extension, to run).

    The dash-c-p-space-dot business is a common way of specifying the classpath. The classpath is the place Java expects to find class files.

    ---

    Step by step instructions for compiling and running a hello world program can be found in the "Hello World!" for Microsoft Windows page of Oracle's Tutorial.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    3therk1ll (June 7th, 2013)

  6. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Main Class not loading

    Got it now, I had the wrong version or type of Java installed *facepalm*, thanks mate.

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM