替换空格和其他字符的当前执行的java文件路径
在我的节目中我做到了
File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath());
execPath = String.valueOf(jarFile.getParentFile());
获取execPath=到当前执行的路径(最终.exe程序所在的位置)。Wich是我的主类的一个属性,我用它来处理和读取一些文件
但是,
在某些带有空格或重音符号的文件夹中运行程序时,execPath的值将替换为%number,如下所示
C:\Users\Hugo\OneDrive%20-%20Funda%c3%a7%c3%a3o%20para%20Inova%c3%a7%c3%b5es%20Tecnol%c3%b3gicas%20-%20FITec\Redes\AutoComRouter\
在哪里
OneDrive%20-%20Funda%c3%a7%c3%a3o%20para%20Inova%c3%a7%c3%b5es%20Tecnol%c3%b3gicas%20-%20FITec
应该是
OneDrive - Fundação para Inovações Tecnológicas - FITec
我想知道是否有可能去掉这个恼人的myexecpath属性替换项
# 1 楼答案
不要调用URL。getPath()方法。正如您现在看到的,它不会返回有效的文件名。它只返回URL的路径部分,即方案和主机之后的部分,所有百分比都会原封不动地转义
将URL转换为文件的正确方法是converting it to a URI,然后constructing a File from that URI
因此,正确的代码如下所示:
注意检查nullThe code source can be null,这取决于类加载器愿意并且能够将多少信息传递给它创建的ProtectionDomain。这意味着试图找到一个目标的位置。运行时的jar文件不可靠
如果要将本机可执行文件与。jar文件,这里有一些更可靠的选项:
第一种方法将使用系统来确定目录,因为是可靠的,并将其传递给程序。例如,在Windows中:
在Linux中:
第二种方法需要创建您的应用程序。jar文件,其中包含可执行文件,然后在外部复制它们。当您想要运行它们时,可以使用jar: