Results 1 to 7 of 7
  1. #1
    swutter is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Brand new user question

    Hello all,

    I'm brand new to java and having trouble running something I wrote. Its a simple program that seems to compile properly but I can't run it. Hopefully someone can help me out. Here is the program:

    import java.util.Scanner;

    public class FirstApp {
    public static void main(String[] args) {
    // Scanner creation
    Scanner input = new Scanner(System.in);

    //Prompt user for base and length
    System.out.print("Enter base then length: ");
    double base = input.nextDouble();
    double length = input.nextDouble();

    //Calculate area of a triangle
    double area = 0.5 * (base * length) ;

    //Display answer
    System.out.println("The area of your triangle is " + area);
    }
    }

    It compiles without errors so I go to the command prompt and go to the folder it's saved in.
    C:\<path to folder>\FirstApp>java FirstApp

    I get the following error whenever I try it:
    Error: Could not find or load main class FirstApp

    I'm using NetBeans 7.1.1 with Java 7 update 3 and JDK 7.3

    Any ideas I would greatly appreciate it.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Brand new user question

    Do a dir command in the folder where you use the java command to see what files are there. There needs to be a FirstApp.class file.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Brand new user question

    There is a similar question here:
    Java problem: Could not find main class HelloWorld - Stack Overflow

    The answer seems to be that you need to set your classpath

    You should also read this because you may have put the main class in a package so you should run it from the package:
    http://www.jarticles.com/package/package_eng.html
    Last edited by ozzyman; 03-23-2012 at 03:16 AM.

  4. #4
    swutter is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Brand new user question

    Thanks there wasn't a file FirstApp.class so I did a search in the folder and subfolders and was able to find it. I found it much deeper: C:\<path to folder>\FirstApp\build\classes\FirstApp\FirstApp.c lass

    So I type in java FirstApp and got the following error:

    Exception in thread "main" java.lang.NoClassDefFoundError: FirstApp
    me: firstapp/FirstApp)
    at java.lang.ClassLoader.defineClass1(Native Method)
    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$100(Unknown Source)
    at java.net.URLClassLoader$1.run(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)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)

    Looks like there might be something I didn't install?

    Thanks in advance!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Brand new user question

    Does the source file have a package statement? That will change how you execute it. You have to include the package name with the classname and set the classpath to the package folder.
    For simpler testing, remove the package statement, recompile and try to execute again.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Brand new user question

    Your Netbeans output path look like classes\FirstApp\FirstApp.class. It look like that your class is inseide a FirstApp package. So to run your code you should start from the classes directory and type something like:

    Java Code:
    java FirstApp.FirstApp

  7. #7
    swutter is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Brand new user question

    Quote Originally Posted by wsaryada View Post
    Your Netbeans output path look like classes\FirstApp\FirstApp.class. It look like that your class is inseide a FirstApp package. So to run your code you should start from the classes directory and type something like:

    Java Code:
    java FirstApp.FirstApp
    This worked, thank you. I was trying to just run the class file but NetBeans did give a path and command to use to run the program. That is odd because its now how the book I'm reading said to run programs. Thanks, I'll pay more attention to what the program tells me instead of the book since its not about NetBeans.

    Thanks for the help all, I'm sure the classpath solution would of worked as well but I didn't quite understand it.

    Baby steps, really tiny baby steps.

Similar Threads

  1. Help please?// Brand new here.
    By GoodThing007 in forum New To Java
    Replies: 10
    Last Post: 12-04-2011, 09:08 PM
  2. Brand New To Java, what does it actually do?
    By Geslincm in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 07:14 PM
  3. Replies: 2
    Last Post: 11-06-2011, 08:32 AM
  4. Very basic question regarding user input
    By fugazi in forum New To Java
    Replies: 6
    Last Post: 01-06-2011, 07:40 PM
  5. BRAND new to Java (editor question)
    By hayden06f4i in forum New To Java
    Replies: 2
    Last Post: 10-20-2010, 10:11 PM

Posting Permissions

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