简体   繁体   English

如何在Java文件中从Eclipse查找Web项目路径?

[英]How to find web project path from Eclipse in Java file?

I want to get the eclipse project path in java.i have one java file using web project in eclipse.in that web project i created folder like splittedfiles. 我想在java.i中获取eclipse项目路径。我在eclipse.web中使用Web项目创建了一个Java文件,在该Web项目中,我创建了类似于splittedfiles的文件夹。 so i want get this folder path.. 所以我想得到这个文件夹路径。

String path = System.getProperty("user.dir");
String p = path + "\\" + "splittedfiles\\";
System.out.println("path " + path);
System.out.println("file path " + p);

whenever i try to execute the above code in my java page its getting software installed path only. 每当我尝试在我的Java页面中执行以上代码时,它只会获取安装路径。 i run this on apache tomcat server 我在apache tomcat服务器上运行

Try this if I understood your question: 如果我了解您的问题,请尝试以下操作:

this.getClass().getResource("/").getPath();

SRC/ folder is compiled to BUILD/CLASSES/, so all you put in src/* goes to classes/* SRC /文件夹被编译为BUILD / CLASSES /,因此,您在src / *中放置的所有内容都将归入classes / *

which is the root of any java application at any IDE. 这是任何IDE上任何Java应用程序的根。

Ok for find folder under webcontent directory use following 可以在webcontent目录下查找文件夹,使用以下

  String a = this.getClass().getResource("/").getPath();
  System.out.println("a=> "+a.replaceAll("/build/classes/", "/splittedfiles"));

May be this will help.. 可能会有所帮助。

i tried this also ...but it will show the same path 我也尝试过...但是它将显示相同的路径

a=>/D:/Eclipse%20Running%20project/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MultiCloud_Architecture/WEB-INF/classes/

use this for updating the path you are looking for 使用它来更新您正在寻找的路径

  System.out.println("a=> "+a.replaceAll("/WEB-INF/classes/", "/splittedfiles"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM