错误:无法找到或加载主类?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,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)

3 years ago

好的,让我更清楚我建议你应该做些什么 .

修改你这样的代码

// 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)));

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

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

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 .

the pattern from your code, via 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));

output

[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

solution

删除 jaro 选项前面的 (EN DASH)并再次输入(作为 HYPHEN-MINUS ) .