Ich versuche, den Pfad des aktuellen laufenden/debuggenen Projekts programmgesteuert in Java zu finden. Ich habe in Google gesucht und was ich gefunden habe, war System.getProperty("user.id")
, was mir den Pfad des Projekts nicht brachte.
Ich weiß, dass der Befehl Environment.currentDirectory
in C # den Pfad des aktuell laufenden/debugged-Projekts angibt. Ich bin mir sicher, dass es auch in Java einen ähnlichen Weg geben muss.
Ich frage mich also, ob mir jemand sagen oder mir einen Code geben könnte, um den Pfad des aktuell laufenden/debugierten Projekts programmgesteuert zu finden.
edit : Ich schreibe Plugin, das mit Eclipse Start geladen wird Das Plugin fügt der Workbench von Eclipse einen Button hinzu, mit dem Code des Plugins Wenn ich auf den Button klicke, suche ich für den aktuellen Verzeichnispfad.
wenn ich zum Beispiel ein Projekt debugge und dann auf die Schaltfläche klicke, wird ein Fenster angezeigt, in dem der Pfad des debugierten Projekts angezeigt wird
Zwei Wege
System.getProperty("user.dir");
oder dieses
File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch block
Die Idee ist, den aktuellen Ordner mit "." Abzurufen, dann die absolute Position abzurufen und den Dateinamen daraus zu entfernen, also von etwas wie
/home/shark/Eclipse/workspace/project/src/com/package/name/bin/Class.class
wenn Sie Class.class entfernen, erhalten Sie
/home/shark/Eclipse/workspace/project/src/com/package/name/bin/
was ist irgendwie was du willst.
Verwenden Sie die user.dir
-Taste, um den Projektstandort mit Java abzurufen.
Code:
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));
Ausgabe:
Present Project Directory :C:\workspace\MyEclipse 10\examples\MySqlMavenDomain
Zur Anzeige aller Systemeigenschaften verwenden Sie einfach System.getProperties();
Verwenden Sie Key
in System.getProperty("Java.version");
, um die Ausgabe abzurufen.
Code:
Properties properties = System.getProperties();
Enumeration<Object> enumeration = properties.keys();
for (int i = 0; i < properties.size(); i++) {
Object obj = enumeration.nextElement();
System.out.println("Key: "+obj+"\tOutPut= "+System.getProperty(obj.toString()));
}
Ausgabe:
Key: Java.runtime.name OutPut= Java(TM) SE Runtime Environment
Key: Sun.boot.library.path OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\bin
Key: Java.vm.version OutPut= 24.45-b08
Key: Java.vm.vendor OutPut= Oracle Corporation
Key: Java.vendor.url OutPut= http://Java.Oracle.com/
Key: path.separator OutPut= ;
Key: Java.vm.name OutPut= Java HotSpot(TM) Client VM
Key: file.encoding.pkg OutPut= Sun.io
Key: user.country OutPut= US
Key: user.script OutPut=
Key: Sun.Java.launcher OutPut= Sun_STANDARD
Key: Sun.os.patch.level OutPut= Service Pack 3
Key: Java.vm.specification.name OutPut= Java Virtual Machine Specification
Key: user.dir OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain
Key: Java.runtime.version OutPut= 1.7.0_45-b18
Key: Java.awt.graphicsenv OutPut= Sun.awt.Win32GraphicsEnvironment
Key: Java.endorsed.dirs OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\endorsed
Key: os.Arch OutPut= x86
Key: Java.io.tmpdir OutPut= C:\DOCUME~1\UDAYP~2.PBS\LOCALS~1\Temp\
Key: line.separator OutPut=
Key: Java.vm.specification.vendor OutPut= Oracle Corporation
Key: user.variant OutPut=
Key: os.name OutPut= Windows XP
Key: Sun.jnu.encoding OutPut= Cp1252
Key: Java.library.path OutPut= C:\Program Files\Java\jdk1.7.0_45\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/MyEclipse/Common/binary/com.Sun.Java.jdk.win32.x86_1.6.0.013/jre/bin/client;C:/Program Files/MyEclipse/Common/binary/com.Sun.Java.jdk.win32.x86_1.6.0.013/jre/bin;C:/Program Files/MyEclipse/Common/binary/com.Sun.Java.jdk.win32.x86_1.6.0.013/jre/lib/i386;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\WindowsPowerShell\v1.0;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;D:\Program Files\Microsoft SQL Server\100\Tools\Binn\;D:\Program Files\Microsoft SQL Server\100\DTS\Binn\;.
Key: Java.specification.name OutPut= Java Platform API Specification
Key: Java.class.version OutPut= 51.0
Key: Sun.management.compiler OutPut= HotSpot Client Compiler
Key: os.version OutPut= 5.1
Key: user.home OutPut= C:\Documents and Settings\uday.p.PBSYSTEMS
Key: user.timezone OutPut=
Key: Java.awt.printerjob OutPut= Sun.awt.windows.WPrinterJob
Key: file.encoding OutPut= UTF-8
Key: Java.specification.version OutPut= 1.7
Key: Java.class.path OutPut= C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\test-classes;C:\workspace\MyEclipse 10\examples\MySqlMavenDomain\target\classes;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\jstl\jstl\1.2\jstl-1.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\mysql\mysql-connector-Java\5.1.28\mysql-connector-Java-5.1.28.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\mail\mail\1.4.7\mail-1.4.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-core\4.3.0.Final\hibernate-core-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging\3.1.3.GA\jboss-logging-3.1.3.GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\logging\jboss-logging-annotations\1.2.0.Beta1\jboss-logging-annotations-1.2.0.Beta1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.0.0.Final\jboss-transaction-api_1.2_spec-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\common\hibernate-commons-annotations\4.0.4.Final\hibernate-commons-annotations-4.0.4.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\jboss\jandex\1.1.0.Final\jandex-1.1.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\hibernate\hibernate-entitymanager\4.3.0.Final\hibernate-entitymanager-4.3.0.Final.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-fileupload\commons-fileupload\1.3\commons-fileupload-1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-net\commons-net\3.3\commons-net-3.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-pool\commons-pool\1.6\commons-pool-1.6.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\commons-beanutils\commons-beanutils\1.9.0\commons-beanutils-1.9.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\Apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-orm\3.2.6.RELEASE\spring-orm-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-jdbc\3.2.6.RELEASE\spring-jdbc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-tx\3.2.6.RELEASE\spring-tx-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-web\3.2.6.RELEASE\spring-web-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aop\3.2.6.RELEASE\spring-aop-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-webmvc\3.2.6.RELEASE\spring-webmvc-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-aspects\3.2.6.RELEASE\spring-aspects-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\aspectj\aspectjweaver\1.7.2\aspectjweaver-1.7.2.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-beans\3.2.6.RELEASE\spring-beans-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context\3.2.6.RELEASE\spring-context-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-context-support\3.2.6.RELEASE\spring-context-support-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-core\3.2.6.RELEASE\spring-core-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-expression\3.2.6.RELEASE\spring-expression-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument\3.2.6.RELEASE\spring-instrument-3.2.6.RELEASE.jar;C:\Documents and Settings\uday.p.PBSYSTEMS\.m2\repository\org\springframework\spring-instrument-Tomcat\3.2.6.RELEASE\spring-instrument-Tomcat-3.2.6.RELEASE.jar
Key: user.name OutPut= uday.p
Key: Java.vm.specification.version OutPut= 1.7
Key: Sun.Java.command OutPut= com.uk.mysqlmaven.domain.test.UserLoginDetails
Key: Java.home OutPut= C:\Program Files\Java\jdk1.7.0_45\jre
Key: Sun.Arch.data.model OutPut= 32
Key: user.language OutPut= en
Key: Java.specification.vendor OutPut= Oracle Corporation
Key: awt.toolkit OutPut= Sun.awt.windows.WToolkit
Key: Java.vm.info OutPut= mixed mode, sharing
Key: Java.version OutPut= 1.7.0_45
Key: Java.ext.dirs OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
Key: Sun.boot.class.path OutPut= C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\classes
Key: Java.vendor OutPut= Oracle Corporation
Key: file.separator OutPut= \
Key: Java.vendor.url.bug OutPut= http://bugreport.Sun.com/bugreport/
Key: Sun.io.unicode.encoding OutPut= UnicodeLittle
Key: Sun.cpu.endian OutPut= little
Key: Sun.desktop OutPut= windows
Key: Sun.cpu.isalist OutPut= pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
Für weitere Details siehe Klassensystem
Was ist mit System.getProperty("user.dir")
? Es gibt Ihnen das Arbeitsverzeichnis, von dem aus Ihr Programm gestartet wurde.
Unter Systemeigenschaften im Java-Tutorial finden Sie eine Übersicht über die Systemeigenschaften von Java.
Java-Projekte haben keinen EINEN Pfad! Java-Projects hat mehrere Pfade, auch wenn eine Klasse mehrere Standorte in unterschiedlichen Klassenpfaden in einem "Projekt" haben kann.
Wenn Sie also eine Taschenrechner.jar in Ihrer JRE/lib und eine Taschenrechner.jar mit denselben Klassen auf einer CD haben: Wenn Sie die Taschenrechner.jar von der CD ausführen, übernimmt Java-vm die Klassen von der JRE/lib!
Dieses Problem tritt häufig bei Programmierern auf, die gerne Ressourcen laden, die innerhalb des Projekts bereitgestellt werden. In diesem Fall,
System.getResource("/likebutton.png")
wird zum Beispiel genommen.
Dies ist die neue Methode:
Path root = FileSystems.getDefault().getPath("").toAbsolutePath();
Path filePath = Paths.get(root.toString(),"src", "main", "resources", fileName);
Oder noch besser:
Path root = Paths.get(".").normalize().toAbsolutePath();
Aber ich würde noch einen Schritt weiter gehen:
public String getUsersProjectRootDirectory() {
String envRootDir = System.getProperty("user.dir");
Path rootDIr = Paths.get(".").normalize().toAbsolutePath();
if ( rootDir.startsWith(envRootDir) ) {
return rootDir;
} else {
throw new RuntimeException("Root dir not found in user directory.");
}
}
Dies ist ein Codeausschnitt, um den Pfad des aktuell ausgeführten Webanwendungsprojekts in Java abzurufen.
public String getPath() throws UnsupportedEncodingException {
String path = this.getClass().getClassLoader().getResource("").getPath();
String fullPath = URLDecoder.decode(path, "UTF-8");
String pathArr[] = fullPath.split("/WEB-INF/classes/");
System.out.println(fullPath);
System.out.println(pathArr[0]);
fullPath = pathArr[0];
return fullPath;
}
Quelle: https://dzone.com/articles/get-current-web-application
File currDir = new File(".");
String path = currDir.getAbsolutePath();
System.out.println(path);
Damit wird am Ende .
gedruckt. Zum Entfernen kürzen Sie einfach die Zeichenfolge um ein Zeichen, z.
File currDir = new File(".");
String path = currDir.getAbsolutePath();
path = path.substring(0, path.length()-1);
System.out.println(path);
den aktuellen Projektpfad können Sie verwenden, System.getProperty("user.dir")
in dieser Methode geben Sie den Schlüsselnamen ein, um die verschiedenen Pfade zu erhalten. Wenn Sie den Schlüssel nicht kennen, können Sie alle Eigenschaften von System.getProperties()
finden. und Sie können den Schlüsselnamen manuell finden.
und schreibe System.getProperty("KEY NAME")
und holen sie sich ihren anforderungspfad.