-2

I have been attempting to build a java project for about two hours, and I have found multiple resources telling me this and that about what to do to resolve my ClassNotFoundException exception. None of them are working for me. At first I thought it was an error with how I was exporting my JAR in Eclipse. However, when I tried to run my code in IntelliJ IDEA Community it still would not work.

My code is over 1000 lines, and thus won't work here. What confuses me though is that I only have one file, "KeyStatistics.java". A ClassNotFoundException exception makes sense if I were trying to access a file by the wrong name, but I only have one file here.

I am using JDK 12, and I built the project in IntelliJ idea after not customizing any of the build settings at all. I would love help because right now my only option looks like dissecting each part of my code and that could take several more hours.

Note: when I was using Eclipse, the project could run perfectly fine (because Eclipse doesn't actually build the project when it runs it).

Edit: here is the entire error:

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

P tray
  • 83
  • 1
  • 3
  • No information here. Too abstract for anyone to suggest anything. Add more information about the file and it's content and what exactly are you doing? What is the exception – Abhijith Nagarajan May 10 '19 at 21:46
  • The exception message will tell you *which* class is not found. What does it say? The accompanying stack trace will tell you what code was trying to access the named class. What does it say? – John Bollinger May 10 '19 at 21:47
  • What class is it throwing ClassNotFoundException for? – David Conrad May 10 '19 at 21:47
  • 1
    Additionally, no, you certainly should not post your whole 1000-line class, but that does not mean you cannot construct and present a [mcve], which is what we want anyway. – John Bollinger May 10 '19 at 21:48
  • @JohnBollinger I added the error to my question. – P tray May 10 '19 at 21:49
  • @DavidConrad The error appears to be coming from the same class. Take a look at the error I put in the question. – P tray May 10 '19 at 21:50
  • The error cannot be coming from your class, because the VM is not successfully loading your class. This is because either it does not exist -- maybe you assigned it to a package, whereas you are trying to refer to it by an unqualified name; maybe the class name is misspelled in the file; maybe ... -- or perhaps it is not in the classpath. There are too many possibilities, so again, present an MCVE. – John Bollinger May 10 '19 at 21:55
  • How are you running this? You said you're building a jar. What's the command line? – David Conrad May 10 '19 at 22:02
  • @DavidConrad First, I'm not running it because it has the error. Second, I'm not sure what the command line is because IntelliJ and Eclipse were both doing it for me. – P tray May 10 '19 at 22:10
  • I'm completely confused. You said you were exporting a jar from Eclipse. I assumed you were then trying to run that jar, or run *with* that jar on your classpath. You're going to have to provide a clearer description of what you're actually doing for anyone to help you. – David Conrad May 10 '19 at 22:37
  • 1
    A [`ClassNotFoundException`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassNotFoundException.html) is thrown at run time, not at compile time. Therefore, _"I'm not running it because it has the error."_ does not make sense to me. What are the steps to reproduce your issue? – howlger May 10 '19 at 23:44
  • @Ptray plase see https://stackoverflow.com/a/42660624/104891. – CrazyCoder May 11 '19 at 00:22

1 Answers1

0

There are two options here:

  1. You are using the single file Java from source, a feature that was added in Java 11.

  2. You are running in the IDE and the compiler output path is mis-configured.

Option 1 - Using the Java 11+ single file feature

In that case you need to specify the file name with the ".java" extension, e.g. from the directory that contains KeyStatistics.java you would run (even if the file specifies a package name):

java KeyStatistics.java

Be sure that you are running Java 11 or newer, in case you have multiple versions installed, e.g.

$JAVA_11_HOME/bin/java KeyStatistics.java

The working directory should be the directory that contains that file, even if it specifies a package. For example, if KeyStatistics.java is in a package org.stats, and normally you would refer to it as org.stats.KeyStatistics, when running a single file from the source you will run it using the directory delimiter, e.g. /, and not the package delimiter ..

So from the root directory of that project you would use

$JAVA_11_HOME/bin/java org/stats/KeyStatistics.java

Option 2 - Mis-configured IDE path:

Open the project structure dialog in IntelliJ-IDEA and ensure that the Project compiler output is configured properly.

Then go into the Modules and select the module that contains your code. Make sure that Compiler output is either set to inherit project path or points to the right directory.

isapir
  • 21,295
  • 13
  • 115
  • 116