简体   繁体   中英

java class path issue

Below is my Java code and and which trying to compile one file. I have two linux setups.

The program is not able to set the class path if I use line 1 and comment line 2. The only difference is quotes.

I have observed that this is machine specific. On some machines this classpath with quotes works fine.

I am using Java 1.6 update 24. Any pointers?

I think quotes is for spaces and code should work with quotes.

    //String clspath = '"' + this.getClassPath() + '"';  // 1 line
    //String clspath = this.getClassPath();   // 2 line

    String[] compArgs = new String[] {
                    "-classpath", clspath,
                    "-g",
                    "-d", ruleCompDirName,
                    realclassNameFull};
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(out, true);
    int compileStatus = com.sun.tools.javac.Main.compile(compArgs, pw);

    // Print compiler output
    if (_logger.isDebugEnabled()) {
        String compOutput = new String(out.toByteArray());
        _logger.debug(compOutput);
    }

I guess com.sun.tools.javac.Main.compile ends up by calling an external Process .

When using a command-line argument from shell, you need to specify where it starts and where it ends. It is done with whitespaces. It is why you need to quote all arguments that contain spaces. But when starting a process from Java, every argument has it's own position in the corresponding array, so quotes are not needed.

In most cases, if you quote an argument, quotes will be considered PART of the argument, and not the enclosing characters, so, in your case, the compiler will search for a file starting by " , which is incorrect.

I'm not sure why in some systems it is accepting the quotes, as it should allways fail...

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