Java get java project path

Как найти путь к текущему каталогу проекта в Java (IDE)?

Я пытаюсь программно найти путь к текущему работающему/отлаживаемому проекту на Java, я посмотрел в Google и нашел следующее: System.getProperty(«user.id») , что не дало мне пути проекта.

Я знаю команду Environment.currentDirectory в C# дает путь к текущему работающему/отлаживаемому проекту, поэтому я уверен, что и в Java должен быть аналогичный способ.

Поэтому я спрашиваю, может ли кто-нибудь сказать мне или дать мне код, как программно найти путь к текущему/отлаживаемому проекту?

редактировать: я пишу плагин, который загружается при запуске eclipse. плагин добавляет кнопку в рабочую среду eclipse, а в коде плагина, когда я нажимаю кнопку, я ищу текущий путь к каталогу.

моя цель состоит в том, что когда, например, я отлаживаю проект, а затем нажимаю кнопку, появляется всплывающее окно с указанием пути отлаженного проекта, спасибо

Вам не следует писать плагины для eclipse, если вы не знаете, как получить текущий рабочий каталог. Я действительно не вижу смысла в кнопке «открыть папку проекта», когда перспектива отладчика уже откроет правильный исходный файл и поместит его в нужную строку. Опять же — это ваше время и усилия. Вы просто можете потратить больше времени на изучение eclipse, чем стрелять себе в ногу, замедляя его еще больше с помощью несколько бесполезного плагина 🙂 — Shark

я думаю, что понял, почему примеры не работают, потому что он печатает мне каталог плагинов, а не отлаживаемый проект. Я думаю, что нашел другой способ, спасибо всем за помощь — user1765376

9 ответы

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 

Идея состоит в том, чтобы получить текущую папку с помощью «.», а затем получить для нее абсолютную позицию и удалить из нее имя файла, поэтому из чего-то вроде

/home/shark/eclipse/workspace/project/src/com/package/name/bin/Class.class 

когда вы удалите Class.class, вы получите

/home/shark/eclipse/workspace/project/src/com/package/name/bin/ 

А если я хочу, чтобы путь src был не bin? — Мезио

Второй пункт ответа не является ответом на вопрос, первый да. Проголосовал за этот ответ: stackoverflow.com/a/22011009/1763602 — Марко Сулла

Используйте user.dir ключ для получения местоположения проекта с помощью java.

System.out.println("Present Project Directory : "+ System.getProperty("user.dir")); 

Present Project Directory :C:\workspace\MyEclipse 10\examples\MySqlMavenDomain

Для отображения всех свойств системы просто используйте System.getProperties();

Используйте Key ценность в System.getProperty(«java.version»); для получения вывода.

Properties properties = System.getProperties(); Enumeration enumeration = properties.keys(); for (int i = 0; i < properties.size(); i++) < Object obj = enumeration.nextElement(); System.out.println("Key: "+obj+"\tOutPut gt-block">Выход:

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 

Для получения более подробной информации см. Система классов

Источник

Читайте также:  Rounded progress bar css
Оцените статью