日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java application文件夹_关于java:如何动态获取Spring Boot Application jar的父文件夹路径?...

發布時間:2025/3/20 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java application文件夹_关于java:如何动态获取Spring Boot Application jar的父文件夹路径?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有一個使用java -jar application.jar運行的spring boot Web應用程序。 我需要從代碼動態獲取jar父文件夾路徑。 我該怎么做?

我已經嘗試過了,但是沒有成功。

歡迎使用Stack Overflow,請在該網站上閱讀有關如何提出問題以及如何創建最小,完整和可驗證的示例的信息。 始終保持具體,并在詢問前盡力而為。 在這種情況下,您提到您嘗試過一種解決方案,但是您的錯誤是什么或基本的實現是什么。

如果我從終端使用java -jar application.jar從該問題出現的代碼將返回null。 將代碼放在這里:MyCustomClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();如果我使用Spring Tools Suit IDE播放按鈕運行應用程序,則此代碼的結果將是:D:arquivosrepositoriomyapptrunktargetclasses

好吧,對我有用的是對該答案的改編。

代碼是:

if you run using java -jar myapp.jar dirtyPath will be something close

to this: jar:file:/D:/arquivos/repositorio/myapp/trunk/target/myapp-1.0.3-RELEASE.jar!/BOOT-INF/classes!/br/com/cancastilho/service.

Or if you run from Spring Tools Suit, something like this:

file:/D:/arquivos/repositorio/myapp/trunk/target/classes/br/com/cancastilho/service

public String getParentDirectoryFromJar() {

String dirtyPath = getClass().getResource("").toString();

String jarPath = dirtyPath.replaceAll("^.*file:/",""); //removes file:/ and everything before it

jarPath = jarPath.replaceAll("jar!.*","jar"); //removes everything after .jar, if .jar exists in dirtyPath

jarPath = jarPath.replaceAll("%20",""); //necessary if path has spaces within

if (!jarPath.endsWith(".jar")) { // this is needed if you plan to run the app using Spring Tools Suit play button.

jarPath = jarPath.replaceAll("/classes/.*","/classes/");

}

String directoryPath = Paths.get(jarPath).getParent().toString(); //Paths - from java 8

return directoryPath;

}

編輯:

實際上,如果您使用的是Spring Boot,則可以像這樣使用ApplicationHome類:

ApplicationHome home = new ApplicationHome(MyMainSpringBootApplication.class);

home.getDir(); ? ?// returns the folder where the jar is. This is what I wanted.

home.getSource(); // returns the jar absolute path.

試試這個代碼

public static String getParentRealPath(URI uri) throws URISyntaxException {

if (!"jar".equals(uri.getScheme()))

return new File(uri).getParent();

do {

uri = new URI(uri.getSchemeSpecificPart());

} while ("jar".equals(uri.getScheme()));

File file = new File(uri);

do {

while (!file.getName().endsWith(".jar!"))

file = file.getParentFile();

String path = file.toURI().toString();

uri = new URI(path.substring(0, path.length() - 1));

file = new File(uri);

} while (!file.exists());

return file.getParent();

}

URI uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();

System.out.println(getParentRealPath(uri));

File file = new File(".");

logger.debug(file.getAbsolutePath());

這對我很有用,可以找到我的jar運行的路徑,希望這就是您所期望的。

此代碼將返回我發出java -jar命令的文件夾路徑。 如果我從我的目標文件夾運行它:cd D: arquivos repositorio myapp trunk target java -jar application.jar我會得到以下結果:D:\arquivos

epositorio\myapp\trunk\target\.如果我是從一個外部文件夾運行,請說:cd D : arquivos repositorio myapp trunk java -jar target application.jar不適:D:\arquivos

epositorio\myapp\trunk\.它部分解決了我的問題。 即使我從另一個外部文件夾發出命令,也可以找到路徑嗎?

這會不會繼續創建的新文件。 每次運行腳本?

總結

以上是生活随笔為你收集整理的java application文件夹_关于java:如何动态获取Spring Boot Application jar的父文件夹路径?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。