exception in thread 'main' java.lang.NoClassDefFoundError:

后端 未结 23 3348
心在旅途
心在旅途 2020-11-28 06:27

The following program is throwing error:

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


        
相关标签:
23条回答
  • 2020-11-28 06:27

    Easy & Simple solution: I solved this problem (NetBeans) by exporting the original project into zip file, deleting the original project directory and importing the project back from the zip file.

    0 讨论(0)
  • 2020-11-28 06:28

    See http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/.

    This is the long form of the Java commands that can be run from a Windows command prompt:

    "C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
    "C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
    
    1. These commands can be run from any directory, meaning you don't have to be in the directory where your HelloWorld.java file is.
    2. The first line compiles your HelloWorld.java file, creating a HelloWorld.class file.
    3. The second line runs the HelloWorld.class file.
    4. The -classpath tells java where to look for the specified file in each command
    5. The Java compiler (javac.exe) expects the location of the java file, relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.java)
    6. Java (java.exe) expects the package (ie com.mycompany.myapp) and class (HelloWorld), relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.class)

    Notice the classpath has no slash at the end. The javac.exe commands expects the file to end with ".java". The java.exe command expects the full class name and does not end with ".class".

    There are a few ways to simplify these commands:

    1. You don't have to specify the entire path to java.exe. Add Java to the Windows Path (Run->sysdm.cpl->Advanced Tab->Environment Variables->Select Path->Edit->Append ";C:\Program Files\Java\jdk1.6.0_18\bin\"). Or you can append JAVA_HOME and create that Environment Variable.
    2. You don't have to enter the entire classpath (ie, you can just use -classpath "."). Enter the directory you will be working in:

      cd "C:\Users\Scott\workspace\myproject\"

    3. You can use the default package (put the HelloWorld.java file directory in your working directory and don't use the Java package directive)

    If you make these changes you would run something like this (and you might be able to leave out -classpath "."):

    cd "C:\Users\Scott\workspace\myproject\"
    javac -classpath "." HelloWorld.java
    java -classpath "." HelloWorld
    
    0 讨论(0)
  • 2020-11-28 06:29

    Try doing

    javac Hello.java
    

    and then, if it comes up with no compiler errors (which it should not do because I cannot see any bugs in your programme), then type

    java Hello

    0 讨论(0)
  • 2020-11-28 06:32

    type the following in the cmd prompt, within your folder:

    set classpath=%classpath%;.;
    
    0 讨论(0)
  • 2020-11-28 06:32

    The Problem here is the setting the environment and the running of the class file. a. To set the environment path run the following command: set path=C:\Program Files (x86)\Java\jdk1.7.0\bin b. run the program from the package like com.test.TestJavaClass

    Command: java com.test.TestJavaClass

    The general issue here is we run it either from inside the package like src/package/name. We should not include src, package name is enough.

    0 讨论(0)
  • 2020-11-28 06:32

    If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:

    Compile: javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java

    Execute: java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics

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