0

So I'm completely new to programming, and I've been writing some Java with the NetBeans IDE. My code runs fine within NetBeans, but I've tried to run it using the command line as well. However, if I run it from the command line, I have to delete the line:

package firstprogram;

which NetBeans automatically places at the top of each new file, or I get the error:

Error: Could not find or load main class FirstProgram

However, if I do delete the line, then the program no longer runs within NetBeans! It doesn't seem right that I have to choose whether to run a .java from within NetBeans or without.

The research I've done makes me think that this is something to do with directory structure? But everything I read on that goes straight over my head. NetBeans has a structure with "build", "dist", "nbproject", and "src", but when I use the command line I just place the .java file in an empty directory and javac from there.

Any explanation is appreciated! The books and tutorials I'm learning from either assume you're just using NetBeans or don't have the package line at all.

M Brown
  • 3
  • 3

3 Answers3

1

You can compile your class using javac command from anywhere, as long as you provide correct relative or absolute path. The problems come when you want to run your program using the java program.

You have to provide the correct path corresponding to your package declaration. For example, if I had 'MyClass' in package mypackage, first line would look like this:

package mypackage;

class source stored on disk:

c:/MyNetbeansProject/src/mypackage/MyClass.java

Compiled bytecode:

c:/MyNetbeansProject/build/classes/mypackage/MyClass.class

Now, if I would have opened a command prompt/terminal in folder c:/MyNetbeansProject/build/classes/, I could run the program using java mypackage/MyClass or java mypackage.MyClass.

However, if i would be somewhere else, I would have to say where the class files are located using the cp option: java -cp c:/MyNetbeansProject/build/classes mypackage/MyClass. The path in cp option can be relative or absolute, use "" when it contains spaces.

kajacx
  • 12,361
  • 5
  • 43
  • 70
0

Package are directory architecture. If your class is in the package com.acme.test, the class should be in the com/acme/test directory. Instead of placing your class in an empty folder, place it in a folder named firstprogram and do javac firstprogram/youclass.java

The package (and folder) permit you to arrange your architecture with logical pattern.

More info here : http://www.tutorialspoint.com/java/java_packages.htm

OcterA
  • 111
  • 2
  • 12
0

So like OcterA said, you should keep organized, but with only one class this is not the issue. I believe that your problem is that you are not entering the correct command into the command line.

First cd to the correct directory and when you want to execute a file within a package in that directory you need to enter

java packageName.className

In this case

java firstprogram.FirstProgram
bpgeck
  • 1,592
  • 1
  • 14
  • 32