简体   繁体   中英

java compile command line

I wrote a program in the Intellij IDEA. It runs fine in the IDE but I am having problems with it from the command line (I so very rarely run my own from the command line I think I am just missing a step.) So here is the problem. If I navigate to where my file is:

cd averageAmpCov/src/

and then run my program it works fine:

java averageAmpCov

Error: Parameter 'out' is required.
Error: Parameter 'in' is required.
Usage: java averageAmpCov
       (-o|--outputlocation) <out> (-i|--inputlocation) <in>

  (-o|--outputlocation) <out>
        Where is the input file to be put? Full path and desired file name

  (-i|--inputlocation) <in>

But when I try to run from outside the folder like this:

java averageAmpCov/src/averageAmpCov

I get a sad error:

Exception in thread "main" java.lang.NoClassDefFoundError: averageAmpCov/src/averageAmpCov (wrong name: averageAmpCov)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

Some other posts say that it might be a class path problem, but I am not really understanding what to do. What am I doing wrong here?

When attempting to run your program outside the class folder, set the classpath to the directory in which your .class file is located:

java -cp averageAmpCov/src averageAmpCov

Now you won't get a NoClassDefFoundError . The classpath is where Java looks to find .class files to load and run. The class name argument must be the actual class name, no directories involved.

To use java correctly, you should ensure that:

  1. You use the fully-qualified class name of the main class.
  2. The main class should be in the classpath.

The classpath can be set in 3 ways:

  1. It can be set within the environment variable CLASSPATH .
  2. With the option -cp which would overwrite CLASSPATH .
  3. If you don't use neither, then the current directory is the classpath.

Note that if the structure of the files as follows:

bin -
     |
     com -
         |
         mypackage -
                   |
                   Hello.class

and Hello 's package name is com.mypackage , then the following command should work:

java -cp C:\projects\myprojct\bin com.mypackage.Hello

Alternatively, you can cd to C:\\projects\\myprojct\\bin and just use:

java com.mypackage.Hello

Running this is fine

java averageAmpCov/src/averageAmpCov

BUT i guess u forgot to set ur classpath set class path first

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