5

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?

3
  • 6
    Is your class file called Main.java, or HelloWorld.java? Commented Mar 5, 2014 at 15:20
  • How are you trying to run this code? Also I don't see any package in your code while error says that you are trying to run Main class from com.company package. Commented Mar 5, 2014 at 15:21
  • you do not need to call main directly. your class name is HelloWorld. JVM calls main method inside it. so just run as "java HelloWorld" Commented Mar 5, 2014 at 15:22

6 Answers 6

4

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"...

Sign up to request clarification or add additional context in comments.

1 Comment

Yes but how to tell IntelliJ to run another main method?
4

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

Comments

3

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.");
    }
}

1 Comment

Or just package company;, if com is the project name.
0

Check these steps in order :

1) Is your file name and your class name is same ?

2) If you work on command line write javac yourfilename.java

3) Then java yourfilename

Comments

0

First Set JDK PATH ..Then compile and run code 

JDK Path Setting:

Follow the given steps below to set the PATH

  1. After installing JDK, right click “My Computer” icon
  2. Choose “Properties” from context menu
  3. Click “Advanced” tab and the click ‘Environment Variable’
  4. In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value. For example, if you have installed JDK in C drive then path may be in the form C:\Program Files\Java\jdk1.6.0_12\bin
  5. You will have to give your own path in which you have installed JDK.
  6. Open Command prompt window, and run your Java code.

Comments

0

Change your main class from com.company.Main to the class you declared

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.