Results 1 to 7 of 7
Thread: Brand new user question
- 03-23-2012, 03:45 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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.
- 03-23-2012, 03:57 AM #2
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.
-
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.htmlLast edited by ozzyman; 03-23-2012 at 04:16 AM.
- 03-23-2012, 04:13 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
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!
- 03-23-2012, 04:15 AM #5
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.
- 03-23-2012, 04:19 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
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
Website: Learn Java by Examples
- 03-23-2012, 04:32 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Brand new user question
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
-
Help please?// Brand new here.
By GoodThing007 in forum New To JavaReplies: 10Last Post: 12-04-2011, 10:08 PM -
Brand New To Java, what does it actually do?
By Geslincm in forum New To JavaReplies: 1Last Post: 11-10-2011, 08:14 PM -
Question about allowing the user jump back to the start of the program? Help please!
By JavaProg in forum New To JavaReplies: 2Last Post: 11-06-2011, 09:32 AM -
Very basic question regarding user input
By fugazi in forum New To JavaReplies: 6Last Post: 01-06-2011, 08:40 PM -
BRAND new to Java (editor question)
By hayden06f4i in forum New To JavaReplies: 2Last Post: 10-20-2010, 11:11 PM
Bookmarks