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?
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"...
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:
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.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With