如何在Java文件中找到Eclipse中的Web项目路径?

时间:2021-08-16 11:39:03

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. so i want get this folder path..

我想在java.i中获取eclipse项目路径,在eclipse.in中使用web项目有一个java文件,我在web项目中创建了像splittedfiles这样的文件夹。所以我想得到这个文件夹路径..

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. i run this on apache tomcat server

每当我尝试在我的java页面中执行上面的代码时,它只获得软件安装路径。我在apache tomcat服务器上运行它

1 个解决方案

#1


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目录下的find文件夹使用以下

  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"));

#1


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目录下的find文件夹使用以下

  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"));