Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Hello World Error

Tags:

java

I'm currently learning Java. My background is primarily in C#. I'm trying to do a basic "hello world". Currently, i've written the following code in IntelliJ:

import java.io.Console;

public class HelloWorld {

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

My app compiles successfully. However, when I attempt to run it, I get a runtime error that says:

Exception in thread "main" java.lang.ClassNotFoundException: com.company.Main
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)

Process finished with exit code 1

I don't understand what's happening. Can someone please help me?

like image 385
user3111277 Avatar asked Mar 05 '14 15:03

user3111277


3 Answers

It seems like you told IntelliJ to call the main method of class com.company.Main. But IntelliJ cannot find such a class. IntelliJ searches for a file Main.class in a folder com/company

Probably you want to tell IntelliJ to run the main method of "HelloWorld" instead of "com.company.Main"...

like image 64
slartidan Avatar answered Oct 16 '22 15:10

slartidan


Go into Edit Configurations (see screenshot) and update your Main class path to be that of the changed package name. In my case, package is "thread" and in there I have a Main class: enter image description here

like image 4
Dodi Avatar answered Oct 16 '22 15:10

Dodi


You need to specify the package inside your class like this

package com.company;

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("hello world.");
    }
}
like image 3
Salah Avatar answered Oct 16 '22 15:10

Salah