Reputation: 12424
Something strange happens when I try running a java -jar
command programatically on Windows.
When running the command like this:
Process p = Runtime.getRuntime().exec(cmd); //cmd string is ok
p.waitFor();
I get the error
Error: Could not find or load main class �jar
The cmd
var is constructed like this:
cmd = String.format("java –jar %s –o win -i %s -n %s -d %s -s %s", jarLocation, param1, param2, tgtFilePath, srcFilePath);
I tried debugging this issue so I copy pasted the command to CMD and got the same error. However, when I typed it manually and not copy pasted, everything just worked fine.
When getting the bytes of each String I got:
bytes of generated 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]
bytes of hard coded string:
[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]
The jarLocation is the one parameter that is already hard coded and not received as a web service parameter.
What can cause an issue like that?
Upvotes: 1
Views: 1249
Reputation: 22963
Ok to make it more clear what I proposed you should do.
Amend you code like this
// 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)));
Then you compare the bytes and have at least the difference. Based on this you can investigate further.
update
Find a short example code for visualising few different dashes, which look quite similar on some fonts.
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));
}
}
}
You need to check the code which generates the value for jarLocation
. As the option -jar
and -o
contain a EN DASH
instead of the 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]
Which shows the dash before jar
and o
is not a HYPHEN-MINUS
solution
Remove the –
(EN DASH) in front of the jar
and o
option and type it again (as HYPHEN-MINUS
).
Upvotes: 1