Jump to content

IntelliJ refuses to run a simple Java code

Go to solution Solved by Eigenvektor,
42 minutes ago, ApexArticO5 said:

Hmm, for me it doesn't create the directory of "scr/main/java". If I launch it without sample code It just has "src" without anything inside it and if I launch it with sample code its puts a main.java file directly inside of "src". 

 

Maybe I need to get an older version?

I'm on the latest version of IntelliJ (2024.2.1). I'm on Linux and using Java 21 (LTS), rather than Java 22, and also using the Ultimate Edition. But none of that should really make much of a difference here. My suspicion is that it has to do with the project's location.

 

Looks like the difference in file structure is down to us selecting different build systems when creating a new project. You selected IntelliJ as the build system, while I selected Gradle (which is what we use at work). If I create a new Java project with IntelliJ as the build system, I get the same file structure you do. However, I'm still able to run the code afterwards.

 

Top right I can see that yours says "Main" next to the run button, while mine says "Current File", so that's one difference. My primary suspicion would be that it has issues with the non-latin characters in your path. Maybe try creating the project in a different directory.

 

If I run the code, this is the command line that I get:

/usr/lib/jvm/default-runtime/bin/java \
 -javaagent:/opt/intellij-idea-ultimate-edition/lib/idea_rt.jar=42941:/opt/intellij-idea-ultimate-edition/bin \
 -Dfile.encoding=UTF-8 \
 -classpath /home/<username>/git/untitled/out/production/untitled \
 Main

 

If I copy this line and run it in the terminal, I get the expected "Hello world!" output.

 

Take a closer look at your output, specifically the end of this line here ("C:\Program Files\Java\…\java.exe"). That's how it tries to run the code.

image.thumb.png.3135f2296792fd79b417118448c5d83e.png

 

I assume if you copy and paste this line and run it in the Windows terminal, you'll get the same error. Meaning something in that line is broken. If you can figure out what it is (path?) that should help point towards how to fix it.

Hello, I'm super new to using Java or its IDE, in the past I've had some experience dabbling into C++, but other than that my programming understanding is minimal. I'm currently in University studying Computer Systems and we are starting to learn basic Java. Now I'm trying to make a simple application that prints "Hello World!", but i cant get it to work through an IDE. I also tried Eclipse, with similarly bad results. Although It works fine with the same code if I compile it through CMD and run it myself.

 

The code, in case of some mistake i made:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}

This should be as simple as it gets...

 

Instead when I try to run I get the following error:

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

 

For the folder location and path I have a folder on desktop called "Programming" in which I have a separate folder for the 1st lecture. The HelloWorld.java is inside src folder. 

 

Thank you beforehand, any help will be appreciated!

Link to comment
https://linustechtips.com/topic/1582639-intellij-refuses-to-run-a-simple-java-code/
Share on other sites

Link to post
Share on other sites

Your class seems to be missing a package. Where/how did you configure the class that IntelliJ is supposed to run? The error says it can't find the class, so likely the configuration that's supposed to point to the class is wrong.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

31 minutes ago, Eigenvektor said:

Your class seems to be missing a package. Where/how did you configure the class that IntelliJ is supposed to run? The error says it can't find the class, so likely the configuration that's supposed to point to the class is wrong.

Can you please elaborate? I didn't think a hello world application needed to import a package? What do you mean by where and how did i configure it? Do you mean the folder arrangement? Did i have to have a script that runs the class? I guess I really don't understand how Java works. All i have is the default folder with all the stuff that IntelliJ automatically makes and I myself made a single class inside "src" folder and called it "HelloWorld.java". 

 

Also I tried running the code seperatelly using cmd to compile and run and i think I got the same error:

Error: Could not find or load main class Uzdevums1.class
Caused by: java.lang.ClassNotFoundException: Uzdevums1.class

 

In this case the code was:

public class Uzdevums1 {
	
	public static void main(String[] args) {
		
		System.out.print("Hello World");
		
	}
	
}

 

As far as I understood the class name in code has to match the .class name?

 

I watched a tutorial on this, but they didn't seem to do anything different and it worked fine. I guess I'll go search another one up. Sorry if this is a bit dumb, i just don't understand it.

 

PS: Found one video with the same error and he fixed it by matching the code class name to the .class file name, but I already have them matching. I even double checked in and copied/paste them just in case. Still got the error.

 

Update (using CMD):

java Hello.class //Didnt work

//but

java Hello //Does work

//So I can run it in using CMD

 

Link to post
Share on other sites

48 minutes ago, ApexArticO5 said:

Can you please elaborate? I didn't think a hello world application needed to import a package?

I'm not talking about imports. Generally Java classes are located in packages. The name of the package must match the folder it's in. The first line says this class is in the package "org.example", which means the class must also be located in the directory ./org/example/HelloWorld.java

package org.example;

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

 

I don't have too much time right now, unfortunately. I'll elaborate more later, provided nobody else has. But if I'm remembering correctly, IntelliJ (or Eclipse) doesn't like classes that are located in the root "src" directory and not part of a package. It's also not something you would do in a typical Java project.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

50 minutes ago, Eigenvektor said:

I'm not talking about imports. Generally Java classes are located in packages. The name of the package must match the folder it's in. The first line says this class is in the package "org.example", which means the class must also be located in the directory ./org/example/HelloWorld.java

package org.example;

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

 

I don't have too much time right now, unfortunately. I'll elaborate more later, provided nobody else has. But if I'm remembering correctly, IntelliJ (or Eclipse) doesn't like classes that are located in the root "src" directory and not part of a package. It's also not something you would do in a typical Java project.

Oh okay, that made a bit more sense. What would be in above the org? Would it be the project file? I did try this but if i create a separate module org/example It does not let me run the file. 

 

In the study task example they have a Main.java file inside of src folder with the following code:

public class Main {
	public static void main(String[] args) {
    		System.out.println("Whatever you need to print");
    }
}

 

Maybe I'm missing something, but that's what it looks like and there isn't much explanation. 

Link to post
Share on other sites

8 hours ago, ApexArticO5 said:

Oh okay, that made a bit more sense. What would be in above the org? Would it be the project file? I did try this but if i create a separate module org/example It does not let me run the file.

Not sure what exactly you did. I simply created a new Project in IntelliJ, added the Main.java file to the "src/main/java" directory that IntelliJ created for me (and where Java code belongs) and I could immediately execute it as is. You can see that SonarLint is immediately nagging me to move the file into a package, since that's common practice.

Spoiler

image.thumb.png.258e51985f74fd56bab1f512b44707bc.png

 

Here's the same thing, using the org.example package. As a general rule of thumb, the package name starts with a domain name (of a domain you own) in reverse notation. So for example LTT might use something like "com.linustechtips.forum" if they where writing their own forum software in Java. Of course if you don't own a domain you can just make something up.

Spoiler

image.thumb.png.b13609769eb082e78063d1d3ad4a351c.png

 

9 hours ago, ApexArticO5 said:

As far as I understood the class name in code has to match the .class name?

Yes, though you're only working with the .java file directly. The .class file is the compiler output. The people who designed the language were essentially trying to enforce good coding practices. The class "Main" must be inside a file named "Main.java" (and gets compiled into "Main.class"). The package name of a class (e.g. "org.example") matches the folder its located in.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

15 hours ago, Eigenvektor said:

Not sure what exactly you did. I simply created a new Project in IntelliJ, added the Main.java file to the "src/main/java" directory that IntelliJ created for me (and where Java code belongs) and I could immediately execute it as is. You can see that SonarLint is immediately nagging me to move the file into a package, since that's common practice.

  Reveal hidden contents

image.thumb.png.258e51985f74fd56bab1f512b44707bc.png

Hmm, for me it doesn't create the directory of "scr/main/java". If I launch it without sample code It just has "src" without anything inside it and if I launch it with sample code its puts a main.java file directly inside of "src". 

 

This is a example of auto generated project with sample code, i haven't touched anything other than run it:

Screenshot2024-09-15104749.thumb.png.1b2c8b087e5af45ef73afb3995b593e9.png

 

And this is what I get when I try to run it a few times:

Screenshot2024-09-15105505.thumb.png.81bc3e8745613162e87f4191adbc16ee.png

 

Those also are the same errors I get when I try to launch any of my codes. Is there something wrong with my setup of the program or my project location? I have downloaded it more than once with the same issue, reinstalled windows yesterday too for another reason. 

 

Maybe I need to get an older version?

Link to post
Share on other sites

42 minutes ago, ApexArticO5 said:

Hmm, for me it doesn't create the directory of "scr/main/java". If I launch it without sample code It just has "src" without anything inside it and if I launch it with sample code its puts a main.java file directly inside of "src". 

 

Maybe I need to get an older version?

I'm on the latest version of IntelliJ (2024.2.1). I'm on Linux and using Java 21 (LTS), rather than Java 22, and also using the Ultimate Edition. But none of that should really make much of a difference here. My suspicion is that it has to do with the project's location.

 

Looks like the difference in file structure is down to us selecting different build systems when creating a new project. You selected IntelliJ as the build system, while I selected Gradle (which is what we use at work). If I create a new Java project with IntelliJ as the build system, I get the same file structure you do. However, I'm still able to run the code afterwards.

 

Top right I can see that yours says "Main" next to the run button, while mine says "Current File", so that's one difference. My primary suspicion would be that it has issues with the non-latin characters in your path. Maybe try creating the project in a different directory.

 

If I run the code, this is the command line that I get:

/usr/lib/jvm/default-runtime/bin/java \
 -javaagent:/opt/intellij-idea-ultimate-edition/lib/idea_rt.jar=42941:/opt/intellij-idea-ultimate-edition/bin \
 -Dfile.encoding=UTF-8 \
 -classpath /home/<username>/git/untitled/out/production/untitled \
 Main

 

If I copy this line and run it in the terminal, I get the expected "Hello world!" output.

 

Take a closer look at your output, specifically the end of this line here ("C:\Program Files\Java\…\java.exe"). That's how it tries to run the code.

image.thumb.png.3135f2296792fd79b417118448c5d83e.png

 

I assume if you copy and paste this line and run it in the Windows terminal, you'll get the same error. Meaning something in that line is broken. If you can figure out what it is (path?) that should help point towards how to fix it.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

13 minutes ago, Eigenvektor said:

Top right I can see that yours says "Main" next to the run button, while mine says "Current File", so that's one difference. My primary suspicion would be that it has issues with the non-latin characters in your path. Maybe try creating the project in a different directory.

Thanks right on the money here, turns out that the folder I had on Desktop under which was the project contained not Latin character. I changed it, created a new project and it ran fine. 

 

13 minutes ago, Eigenvektor said:

Looks like the difference in file structure is down to us selecting different build systems when creating a new project. You selected IntelliJ as the build system, while I selected Gradle (which is what we use at work). If I create a new Java project with IntelliJ as the build system, I get the same file structure you do. However, I'm still able to run the code afterwards.

Okay, the issue is solved, thanks, wanted to ask though, any reason to use Gradle over IntelliJ for the build system? And I see that there is an option for 2 DLS's underneath Gradle?

Link to post
Share on other sites

1 minute ago, ApexArticO5 said:

Okay, the issue is solved, thanks, wanted to ask though, any reason to use Gradle over IntelliJ for the build system? And I see that there is an option for 2 DLS's underneath Gradle?

Multiple reasons:

  • Gradle includes dependency management (similar to Maven), so if you want to add a library to your project (e.g. Apache Commons IO), all it takes is a simple entry in the build.gradle file to add it. Much simpler than downloading them yourself, making sure they're in the correct directory, keeping them updated and so on
  • It makes your program less dependent on a particular IDE, so e.g. if you later wanted to switch to Eclipse (or vice-versa), you can do that much more easily. At work we had people use Eclipse and IntelliJ for the same project at the same time, before everyone transitioned to IntelliJ
  • It allows us to run automated builds and tests on a build server (e.g. GitHub, GitLab, Jenkins, …)

If you're just starting out, the first point is likely the most important one.

 

The DSL (domain-specific language) defines which programming language you want to use for Gradle's build scripts. Kotlin is the more modern choice.

 

The toggle simply defines whether you want your build scripts to be written in Groovy or Kotlin (build.gradle for Groovy or build.gradle.kts for Kotlin). Kotlin has some nice features, like better auto-complete. It is also more strict in terms of syntax, so there's less ambiguity how things should be done. Also, if you write code in Kotlin (rather than Java), using Kotlin for your build scripts is a more natural choice as well.

 

Groovy often has multiple slight variations of syntax it accepts for the same thing, while Kotlin generally only has one. While the differences are minor, it can quickly lead to inconsistencies when working with a team and you haven't defined/enforced which style you want to go with. Kotlin removes that ambiguity, which is also what enables the better auto-completion features.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×