Unable to run java program with a classpath in a file and usage of @ on the cli

T3rm1 :

I came across a strange behavior when executing a java program from command line with a classpath in a file. I'm not sure if this is related to Windows, Java or the cli.

Test class:

package de.test;

public class Playground {

    public static void main(String[] args) throws Exception {
        System.out.println("hello world");
    }

}

After compiling I can run it with the command

java -classpath "C:\Users\TEST\eclipse-workspace\Playground\target\classes" de.test.Playground

However, if I have a file cp.txt with the content (this is only the classpath argument)

-classpath "C:\Users\TEST\eclipse-workspace\Playground\target\classes"

and I run

java "@cp.txt" de.test.Playground

I get the error java.lang.ClassNotFoundException: de.test.Playground. Same without quotes around @cp.txt.

I tested this with Oracle JDK 11.0.2 and OpenJDK 11.0.5. Both show the same behavior.

Does anyone know why this happens? Whenever the classpath is too long, Eclipse puts it in a file and uses the @ command line to run the program. But this doesn't work as shown above. So whenever I have too many dependencies I'm no longer able to run a Java program from within Eclipse.

howlger :

In your command-line argument file cp.txt escape backslashes (\) by replacing them with double backslashes (\\) or use forward slashes (/) instead of backslashes (\).

See The java Command - java Command-Line Argument Files:

  • Because backslash (\) is an escape character, a backslash character must be escaped with another backslash character.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=359194&siteId=1