0

This week I decided that I wanted to learn Java. To clarify, I am a COMPLETE Novice when it comes to programming. When I say that it's not me being humble about my skills. I've literally just started learning the concepts this week.

The current book I'm using is titles "introduction to Java programming", by Daniel Liang. After a short intro that explains some computer hardware, the book asks to create a simple program that is displayed in the book as:

public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
//Above line displays Welcome to Java on the console
}
}

I've manually typed that code (making sure the letters are capitalized properly) into notepad++ and then into my netbeans IDE. It builds with no errors, but when I try to run it, it says: Error:Could not find or load main class welcome.to.java.WelcomeToJava

I'm working on a windows 10 64 bit laptop. I've installed the jdk/jre. I looked up what could be causing this and basically everything pointed to the paths/classpaths.

I've set my paths already, but I'm getting lost trying to wrap my head around classpaths. EVERY tutorial assumes that I know what all the terms they're using means, or that I know how to use the command prompt. I know nothing. It's starting to frustrate me a little because there don't seem to be any tutorials or explanations out there for people who legitimately are new to this.

I really want to learn Java and I'm not trying to let an error in my first program be the thing that deters me. I've found similar threads but all of the answers kept bringing up other things that I didn't understand such as packages and creating files. It's kind of embarrassing but I really need somebody to hold my hand here.

MNK
  • 1
  • 1
  • What is the name of a file where you saved your code into? – PM 77-1 Feb 28 '17 at 17:43
  • 1
    Your class doesn't have any package statement, and its name is `Welcome`. So it's in the default package. So its fully qualified name is `Welcome`, not `welcome.to.java.WelcomeToJava`. – JB Nizet Feb 28 '17 at 17:45

3 Answers3

1

what is WelcomeToJava here? if it is file name - it should match public class name in file. Also correct package name should be defined.

iMysak
  • 2,170
  • 1
  • 22
  • 35
0

The name of your executable class is "Welcome", not "WelcomeToJava". that is why Java cannot find the class WelcomeToJava.

Change public class Welcome { to public class WelcomeToJava { and it will likely work. Also, given that from your comment it looks like your class should be in a "welcome.to.java" package, add the line

package welcome.to.java;

at the beginning of your code

Lev
  • 317
  • 2
  • 9
0

Your IDE build path needs to be set to the correct location, and your class needs to be in the correct folder/package. And your class file name needs to be Welcome.java

So if your IDE java build path is set to look in the folder 'src' (or whatever folder name you have) then your file needs to be in src. And the folders under there need to match your package declaration.

So for the following, the .java file will need to be 3 folders deep in src/foo/bar (assuming src is where you have the java build path set to).

package foo.bar;

public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
        //Above line displays Welcome to Java on the console
    }
}
MattC
  • 5,874
  • 1
  • 47
  • 40