-1

I'm very new to java(started today), and when I created my first application it generated some code and the first line was "package firstproject;" (without the quotes).

When I tried to run this I kept getting an error that said it couldn't find my first project class?

I created another project and this time it didn't have this package line but did have the other stuff which was public class and stuff and I didn't get the error?

The tutorial I was following said the code should have ran ok without changing it and with that package line?

Any help will be greatly appreciated!!

Thanks

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
macsvv
  • 1
  • 2
  • Any chance you can give us the exact wording of the error? Also What development environment are you using? – Treesrule14 Jun 17 '13 at 23:05
  • Where and how are you running your program? Can you give us the link to that tutorial? – Smit Jun 17 '13 at 23:05
  • Packages are Java's analog to directories in order to keep your classes organized. If you're declaring that class to be part of a package named `firstproject`, it needs to be in a directory named `firstproject` and referred to from outside that directory. I'm not familiar with Netbeans or the tutorial you're using, so I can't give you more specific help than that. – Reinstate Monica -- notmaynard Jun 17 '13 at 23:19
  • @macsvv Without sufficient details its hard to guess whats going wrong and its not worth to keep on guessing as there could be many reason. Try following to solve your problem as you are saying you have followed the tutorial and considering its correct [--> Could not find or load main class](http://stackoverflow.com/questions/7647448/could-not-find-or-load-main-class) – Smit Jun 17 '13 at 23:19
  • Welcome to Stack Overflow macsvv! Although you did describe your problem, it is greatly appreciated to be able to see some code. Consider adding some code so that your question will have a much higher value – Cody Guldner Jun 19 '13 at 19:13

2 Answers2

1

Sounds like you tried to run your project's main class without specifying the package name, for instance assuming your're using the command line, this class:

//Main.java
package firstproject;
public class Main { 
    public static void main( String ... args ) { 
        System.out.println("ding...");
    }
}

Should compile and run without problems like this:

$javac -d . Main.java
$java firstproject.Main
 ding...

The -d flag says to create the package structure in the following directory (.)

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

Might I suggest as a starting point to follow the Hello World application in the Oracle Java tutorials. http://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html

At a minimum you should have a class like the following

public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }

}
Lionel Port
  • 3,492
  • 23
  • 26