javascript
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的父文件夹路径?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 新建double对象_java
- 下一篇: java表单上下左右滚动_怎么在网页中实