简体   繁体   中英

Again “wrong name” error when executing java program

With reference to this post Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line

I did not understand how to solve the problem

Actually in my java source code there' s line :

package es_2011;

when I compile the program through JCreator everything works perfectly. It creates a folder named es_2011 where to put .class files. Also the executing operation goes smoothly, the program runs ok.

Now I'd like to use the command line only. So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error

The command I use is: javac ProgAudioJ.java

The path (where javac.exe is ) is : C:\\Program files\\Java\\jdk1.6.0_22\\bin

Is someone willing to help me understand in terms of exactly tell me what I have to do? thanks very much...MAX

The setup used for the looks like this (under windows)

C:\\classDir -> is the project C:\\classDir\\testpackage -> is the only package used (the package "testpackage") C:\\classDir\\testpackage\\Main.class -> is the class with the main method inside (important: it is the .class and not .java)

The Main.class looks like following:

package testpackage;

public class Main {

    public static void main(String[] args) {
        System.out.println("Program started! ;-)");
    }

}

go with your command prompt to:

c:\classDir> java testpackage.Main

the result:

Program started! ;-)

According to your problems that it starts in your IDE but not from the console: - checked if you realy use the path to the .class files? - with the console go to the directory of you .class files, not the project (eg in Eclipse it is the bin directory - enter the full qualified class name (including packages seperated by . -> eg testpackage.Main

More infos can be found under: http://www.oracle.com/technetwork/java/compile-136656.html

Hope it helped

MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.

So in your case, put the ProgAudioJ.java in the folder es_2011 and then run javac es_2011\\ProgAudioJ.java

latter to run it, you need the command

java es_2011.ProgAudioJ

You should add javac.exe in your path .Edit your path variable and append path to jdk's bin

then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile

c:\es_2011\javac YourJava.java

and now go back to C:

c:\java es_2001.Yourjava

After reading you other Post: " Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line " I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run

java ProgAudioJ

from that folder.

instaend you have to go to the folder above ( cd .. ) and run

java es_2011.ProgAudioJ

Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow . Typically the whole project would have a separate src folder containing com/stackoverflow .

When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe , you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\\Program Files\\something . Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe .

Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow . The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.

Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM