错误:无法找到或加载主类?jar

时间:2015-10-08 10:14:21

标签: java command-line jar

当我尝试在Windows上以编程方式运行java -jar命令时会发生奇怪的事情。

运行如下命令时:

Process p = Runtime.getRuntime().exec(cmd); //cmd string is ok
p.waitFor();

我收到错误

  

错误:无法找到或加载主类�jar

cmd var的构造如下:

cmd = String.format("java –jar %s –o win -i %s -n %s -d %s -s %s", jarLocation, param1, param2, tgtFilePath, srcFilePath);

我尝试调试此问题,因此我将命令复制粘贴到CMD并得到了同样的错误。但是,当我手动键入它而不是复制粘贴时,一切都运行正常。

获取每个String的字节时:

生成的字符串的字节:

  

[106,97,118,97,32,-30,-128,-109,106,97,114,32,110,112,116,95,108,105,99,101,110,115 ,101,46,106,97,114,32,-30,-128,-109,111,32,119,105,110,32,45,105,32,104,111,115,116,105, 100,55,56,57,48,49,32,45,110,32,104,111,115,116,110,97,109,101,32,45,100,32,108,105,99, 101,110,99,101,70,105,108,101,45,105,110,112,117,116,76,105,99,101,110,99,101,45,114,101,115, 117,108,116,46,100,97,116,32,45,115,32,108,105,99,101,110,99,101,70,105,108,101,45,105,110, 112,117,116,76,105,99,101,110,99,101]

硬编码字符串的字节:

  

[106,97,118,97,32,45,106,97,114,32,110,112,116,95,108,105,99,101,110,115,101,46,106, 97,114,32,45,111,32,119,105,110,32,45,105,32,104,111,115,116,105,100,55,56,57,48,49,32, 45,110,32,104,111,115,116,110,97,109,101,32,45,100,32,108,105,99,101,110,99,101,70,105,108, 101,45,105,110,112,117,116,76,105,99,101,110,99,101,45,114,101,115,117,108,116,46,100,97,116, 32,45,115,32,108,105,99,101,110,99,101,70,105,108,101,45,105,110,112,117,116,76,105,99,101, 110,99,101]

jarLocation是一个已经硬编码且未作为Web服务参数接收的参数。

什么可能导致这样的问题?

1 个答案:

答案 0 :(得分:1)

好的,可以更清楚地说明我建议你应该做些什么。

修改你这样的代码

// this will be the command which produces �jar
cmd = String.format("java –jar %s –o win -i %s -n %s -d %s -s %s", jarLocation,
          param1, param2, tgtFilePath, srcFilePath);
System.out.println(Arrays.toString(cmd.getBytes(charsetLatin1)));

// this will be the working command, as you wrote
cmd = "here you type the same command";
System.out.println(Arrays.toString(cmd.getBytes(charsetLatin1)));        

然后你比较字节并至少有差异。基于此,您可以进一步调查。

<强>更新
找到一个简短的示例代码,用于可视化几个不同的破折号,在某些字体上看起来非常相似。

import java.nio.charset.StandardCharsets;

public class Dashes {

    public static void main(String[] args) {
        // see: https://en.wikipedia.org/wiki/Dash
        byte[] enDash = {-30, -128, -109};
        byte[] minusSign = {45};
        byte[] emDash = {-30, -128, -108};

        byte[][] charBytes = new byte[3][];
        charBytes[0] = enDash;
        charBytes[1] = minusSign;
        charBytes[2] = emDash;

        for (byte[] bytes : charBytes) {
            String s = new String(bytes, StandardCharsets.UTF_8);
            char c = s.charAt(0);
            System.out.printf("%d  %s  %s%n", s.length(), c, Character.getName(c));
        }
    }
}

您需要检查生成jarLocation值的代码。由于选项-jar-o包含EN DASH而非HYPHEN-MINUS

代码中的模式,通过copy'n'paste

byte[] b = "java –jar %s –o win -i %s -n %s -d %s -s %s".getBytes();
System.out.println(Arrays.toString(b));

<强>输出

[106,97,118,97,32,-30, -128, -109,106,97,114,32,37,115,32,-30, -128, -109,111,32,119,105,110, 32,45,105,32,37,115,32,45,110,32,37,115,32,45,100,32,37,115,32,45,115,32,37,115]

显示jaro之前的短划线不是HYPHEN-MINUS

<强>溶液

删除jar选项前面的o(EN DASH)并再次输入(HYPHEN-MINUS)。