1
votes

Hello so recently I have started to transfer from c++ to java and one exercise is to compile and run a java program using cmd.

So okay, I coded my simple HelloWorld program using netbeans and saved it,

package helloworld;      
public class Helloworld 
{


   public static void main(String[] args) 
   {
        System.out.println("Hello world");
   } 

}      

so now my saved .java file is in C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src\helloworld

now I open the cmd and I change the directory to the above and typed javac Helloworld.java and now I can see Helloworld.class in the same folder, I read from somewhere that I have to include the package name as well for it to run so I typed

java helloworld.Helloworld

it gave me an error so I tried running it from the src folder instead but it also gave me the same error.

Error: Could not find or load main class Helloworld

Caused by: java.lang.ClassNotFoundException: Helloworld

I have read some possible solutions such as using -cp or using -d but it keeps giving me the same error. I am currently using jdk1.8.0_161. on windows 10.

1
You need to compile at the root of the project. Not from the package.AxelH
By root of the project I assume you mean C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src?eatmybananapls

1 Answers

2
votes

You have to use

java helloworld.Helloworld      

and from the parentfolder of helloworld, which is the src directory, in your case.

There is a tight relationship between package and directory structure.

There are many flags you can set for the compiler, like srcdir, targetdir to keep classes and sources apart. But basically, when you invoke your class helloworld.Helloworld, the JVM looks for a directory helloworld/ and expects a Helloworld.class there.

To achive this without compiler flags, you have to put the source into the helloworld/ folder too.

The whole name of your class is helloworld.Helloworld and java should look there and find it there.

mkdir helloworld
mv Helloworld.java  helloworld/
javac helloworld/Helloworld.java 
java helloworld.Helloworld 
Hello world

It's a bit surprising in the beginning, if you don't know it and started with classes without package declaration. But the logic is simple and straight forward: Every package is reflected by the directory structure.

With a distinction of sourcedir and targetdir, the directory structure below has to be the same as without, just the starting point differs. Common target dirs are classes or bin, like in:

javac -s . helloworld/Helloworld.java -d classes

or

javac -s ./src helloworld/Helloworld.java -d bin

But bin or classes don't get part of the package name, and you can't extend the invocation of the class by prepending that dir to the invocation path:

java bin.helloworld.Helloworld

won't work. But

java -cp ./bin helloworld.Helloworld

should. If you read the documentation carefully, you will find, that it carefully distinguishes source file (Helloworld.java), class (Helloworld) and file (Helloworld.class).