简体   繁体   中英

Using -classpath on terminal doesn't work

When I try using the -classpath option I cannot execute any program.

I can compile it with

javac FirstClass.java -cp C:\Users\pierr\Desktop\Isitgonnawork

but when executing,

java FirstClass -cp .;C:\Users\pierr\Desktop\Isitgonnawork

I get this exception :

    Exception in thread "main" java.lang.NoClassDefFoundError: StdOut
        at FirstClass.main(FirstClass.java:5)
Caused by: java.lang.ClassNotFoundException: StdOut
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)

This happens when the classpath is a jar file and also when it's a simple directory containing java files.

The strange thing is that when I put the path to the directory/jar file in the CLASSPATH environment on Windows, I can compile & run without problem, but not using the -classpath option...

I am on Windows.

Thanks in advance for your help !

The -cp argument must be before the source filenames in javac and the class name and app arguments in java. Please refer to the manual entries for java and javac. – Stephen C

Thanks ! This was indeed what I did wrong.

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