Dear community,
One of the frustrating problems I find with programming is when the compiler or runtime environment can’t find stuff when the location to me is blatantly obvious.
I have followed an introductory course on setting up a Java development environment at Pluralsight, using IntelliJ. At the end was explained in two ways how to create a jar file that is supposed to work stand-alone. The exercise project contains a class called ‘Main’.
The first method was using the command prompt, but it fails because the runtime environment can’t find or load the class called ‘Main’.
The second way was using IntelliJ, but it fails for the same reason.
So I created a new, simpler project, called HelloWorld, with only two files, with also a class called ‘Main’. It fails for the same reason.
Then I tried some exercise on webeducator (that I can't link to for some reason). It contains only a single file and it actually works, despite the erroneous image in step 4.
In my HelloWorld project I have two classes :
package com.example.helloworld;
public class Main { public static void main(String[] args) { HelloWorldCreator hello = new HelloWorldCreator() ; hello.giveGreeting() ; } }
and
package com.example.helloworld; public class HelloWorldCreator { String greeting = "Greetings" ; String addressee = "World"; public void setGreeting(String greeting) { this.greeting = greeting ; } public void setAddressee(String addressee) { this.addressee = addressee ; } public void giveGreeting() { System.out.printf("%s, %s!", greeting, addressee) ; } }
I managed to convert these to .class files using the instruction
javac Main.java HelloWorldCreator.java
in the command prompt.
Issuing the instruction
java Main
in the pertinent folder returns
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: com/example/helloworld/Main (wrong name: Main)
How can explain to the JRE where to find that class ?