【发布时间】:2021-07-12 12:59:39
【问题描述】:
我创建了一个在 Windows 启动时运行的 bat 文件,它会启动我的 jar 文件
start javaw -jar "C:/Users/No%c3%a9/Desktop/Workspace/DTK_LTOM/out/artifacts/DTK_LTOM_jar/DTK_LTOM.jar"(看,这里用 %c3%a9 替换了“é”,当我自己替换“é”时,它仍然用奇怪的符号替换它)
但是我遇到了一个问题:当我启动它时,什么也没有发生。所以我试着写“java”而不是“javaw”来看看问题出在哪里。我有一个包含“é”字符的法语名字,这似乎是问题所在,因为它显示了一个错误对话框窗口,并且“é”被一些奇怪的字符替换。那么我的问题是:如何使 bat 文件或 jar 文件能够使用特殊字符。
public class Listener {
JDA jda;
String roaming = System.getenv("APPDATA");
public Listener() {
//jda = Grabber.getInstance().getJda();
String path = roaming + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\";
File fi = new File(path + "javalaunchauto.bat");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fi));
writer.write("start javaw -jar " + Grabber.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1));
writer.close();
} catch (IOException e) { e.printStackTrace(); }
JFrame frame = new JFrame(); //here just to see when the jar is opened
frame.setVisible(true);
}
public static void main(String[] args) {
new Listener();
}
}
我发现了一些有用的东西:使用class.getProtectionDomain().getCodeSource().getLocation().getPath() 方法是有风险的,如果你在那之后不解码它。我确实使用URLDecoder.decode(str,"UTF-8") 对其进行了解码,并将str 作为我的jar 文件的路径,使用class.getProtectionDomain().getCodeSource().getLocation().getPath()。我以为它会起作用,但实际上它确实在批处理文件中添加了“é”,但打开它时仍然显示错误。 (Error: Unable to access jarfile C:/Users/NoÚ(instead of Noé)/Desktop/etc.)
【问题讨论】:
-
您的代码页呢?如果你在命令提示符窗口中输入
chcp并按下[ENTER]键,它会输出什么? -
它输出“850 个活动代码页”
-
如果可以的话,我想用代码来改变它,因为它必须与任何计算机兼容
-
getLocation().getPath()不返回文件名。您不能以这种方式将 URL 转换为文件。请改用getLocation().toURI(),并将该URI 传递给File构造函数。
标签: java batch-file