How do I resolve ClassNotFoundException?

前端 未结 22 1460
后悔当初
后悔当初 2020-11-21 06:06

I am trying to run a Java application, but getting this error:

java.lang.ClassNotFoundException:

After the colon comes the location of the cla

相关标签:
22条回答
  • 2020-11-21 06:35

    If you have moved your project to new machine or importing it from git, then try this.

    1. Right Click on class > Run as > Run Configuration
    2. remove main class reference
    3. Apply > Close
    4. Now again right click on class > run as java application.

    It worked for me.

    0 讨论(0)
  • 2020-11-21 06:36

    Basic Generic Question - Simplest Generic Answer ;)

    Given the information I will make the assumption that you might be trying a basic approach to coding, building/compiling and running a simple console app like "Hello World", using some simple text editor and some Command Shell.

    This error occurs in the fallowing scenario:

    ..\SomePath>javac HelloWorld.java
    ..\SomePath>java HelloWorld.class
    

    In other words, use:

    ..\SomePath>java HelloWorld
    

    P.S. The adding the file extension .class produces the same mistake. Also be sure to have the Java's (JDK/JRE) bin folder in the operating system's Environment Variables's PATH.(Lookup for more details other posts on this) P.P.S Was I correct in my assumption/s?

    0 讨论(0)
  • 2020-11-21 06:39

    If you know the path of the class or the jar containing the class then add it to your classpath while running it. You can use the classpath as mentioned here:

    on Windows

    java -classpath .;yourjar.jar YourMainClass
    

    on UNIX/Linux

    java -classpath .:yourjar.jar YourMainClass
    
    0 讨论(0)
  • 2020-11-21 06:41

    I just did

    1.Invalidate caches and restart

    2.Rebuilt my project which solved the problem

    0 讨论(0)
  • 2020-11-21 06:41

    If you use maven, check that you have this plugin in your pom.xml:

        <build>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <!-- Attach the shade goal into the package phase -->
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
    

    It will put your dependency (the exception reason) to your jar.

    FYI: this will include all dependencies inflated in the final jar

    0 讨论(0)
  • 2020-11-21 06:42

    If you have added multiple (Third-Party)**libraries and Extends **Application class

    Then it might occur.

    For that, you have to set multiDexEnabled true and replace your extended Application class with MultiDexApplication.

    It will be solved.

    0 讨论(0)
提交回复
热议问题