私がする必要があるのは、実行中の jar/exe ファイルの名前を取得することです (Windows では EXE、Mac/Linux では jar になります)。私は周りを探していましたが、方法を見つけることができないようです。
実行中のJarまたはExeの名前を取得するには?
これがお役に立てば幸いです。コードをテストすると、完全なパスと名前が返されます。
コードでもう少し遊んで、フィードバックをお寄せください。
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
これは、類似しているが == ではない、stackoverflow の質問 How to get path of a running JAR file?で見つかりました。
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());</p>
コンパイルされたアプリケーションで単純なパスを返し、jar でエラーを返します。
URI は階層的ではありません
私の解決策は次のとおりです。
private File getJarFile() throws FileNotFoundException {
String path = Main.class.getResource(Main.class.getSimpleName() + ".class").getFile();
if(path.startsWith("/")) {
throw new FileNotFoundException("This is not a jar file: \n" + path);
}
path = ClassLoader.getSystemClassLoader().getResource(path).getFile();
return new File(path.substring(0, path.lastIndexOf('!')));
}
File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
あなたに瓶を与えるべきです。
exeについては、ある種のラッパーを使用していると想定しているため、実行する前にexeの名前を知る必要があります。次に、次のようなものを使用できます。
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
Lunch4jを使用すると、生成された xml を編集して exe ファイルのイメージ名を追加できます。タグ値 true を false から true に変更する必要があります。