通过java获取当前项目路径

发布网友

我来回答

5个回答

热心网友

getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)

例如 项目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/

public String getCurrentPath(){  
       //取得根目录路径  
       String rootPath=getClass().getResource("/").getFile().toString();  
       //当前目录路径  
       String currentPath1=getClass().getResource(".").getFile().toString();  
       String currentPath2=getClass().getResource("").getFile().toString();  
       //当前目录的上级目录路径  
       String parentPath=getClass().getResource("../").getFile().toString();  
         
       return rootPath;         
  
   }

参考资料:http://blog.csdn.net/hpf911/article/details/5852127

热心网友

File file = new File("test");
String path = file.getAbsolutePath();
System.out.println(path.substring(0, path.lastIndexOf(File.separator)));
file.delete();

热心网友

package application.util;/*** * 获取项目根路径工具类 * */
public class PathUtil {
/**如果没打包后运行则debug为true*/
public static boolean debug = false;
/**项目所在路径*/
public static final String projectPath = initProjectPathAndDebug();
/*** * 获取项目根路径,无论是打包成jar文件。 * 为了保证调试时获取项目路径,而不是bin路径,增加逻辑: 如果以bin目录接,则返回上一层目录 * 例如:F:\eclipseworkJavaFX\PersonalAssistant 后面的bin目录会去掉 * @return 例如:F:\eclipseworkJavaFX\AddressApp\build\dist */
private static String initProjectPathAndDebug(){java.net.URL url = PathUtil.class.getProtectionDomain().getCodeSource().getLocation();
String filePath = null;
try {filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
}catch (Exception e) {e.printStackTrace();
}if (filePath.endsWith(".jar"))
{ filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
}//如果以bin目录接,则说明是开发过程debug测试查询,返回上一层目录
if (filePath.endsWith("bin/") || filePath.endsWith("bin\\") )
{ debug = true; filePath = filePath.substring(0, filePath.lastIndexOf("bin"));}java.io.File file = new java.io.File(filePath);
filePath = file.getAbsolutePath();return filePath;
}
/*** * 这个方法打包位jar文件就无法获取项目路径了。 * @return */
public static String getRealPath() {String realPath = PathUtil.class.getClassLoader().getResource("").getFile();java.io.File file = new java.io.File(realPath);realPath = file.getAbsolutePath();//去掉了最前面的斜杠/
try {realPath = java.net.URLDecoder.decode(realPath, "utf-8");
} catch (Exception e)
{e.printStackTrace();}return realPath;}
public static void main(String[] args) {System.out.println(projectPath);}}

热心网友

xx.class.getProtectionDomain().getCodeSource().getLocation().getPath();
获取 xx这个类文件的绝对路径去看看吧 剩下的就自己取解析吧
request.getContextPath(); 能直接获取

热心网友

String path = "D:/workspace/TEST";
String name = path.substring(path.LastIndexOf ('/'),path.length());

热心网友

getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)

例如 项目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/

public String getCurrentPath(){  
       //取得根目录路径  
       String rootPath=getClass().getResource("/").getFile().toString();  
       //当前目录路径  
       String currentPath1=getClass().getResource(".").getFile().toString();  
       String currentPath2=getClass().getResource("").getFile().toString();  
       //当前目录的上级目录路径  
       String parentPath=getClass().getResource("../").getFile().toString();  
         
       return rootPath;         
  
   }

参考资料:http://blog.csdn.net/hpf911/article/details/5852127

热心网友

File file = new File("test");
String path = file.getAbsolutePath();
System.out.println(path.substring(0, path.lastIndexOf(File.separator)));
file.delete();

热心网友

package application.util;/*** * 获取项目根路径工具类 * */
public class PathUtil {
/**如果没打包后运行则debug为true*/
public static boolean debug = false;
/**项目所在路径*/
public static final String projectPath = initProjectPathAndDebug();
/*** * 获取项目根路径,无论是打包成jar文件。 * 为了保证调试时获取项目路径,而不是bin路径,增加逻辑: 如果以bin目录接,则返回上一层目录 * 例如:F:\eclipseworkJavaFX\PersonalAssistant 后面的bin目录会去掉 * @return 例如:F:\eclipseworkJavaFX\AddressApp\build\dist */
private static String initProjectPathAndDebug(){java.net.URL url = PathUtil.class.getProtectionDomain().getCodeSource().getLocation();
String filePath = null;
try {filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
}catch (Exception e) {e.printStackTrace();
}if (filePath.endsWith(".jar"))
{ filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
}//如果以bin目录接,则说明是开发过程debug测试查询,返回上一层目录
if (filePath.endsWith("bin/") || filePath.endsWith("bin\\") )
{ debug = true; filePath = filePath.substring(0, filePath.lastIndexOf("bin"));}java.io.File file = new java.io.File(filePath);
filePath = file.getAbsolutePath();return filePath;
}
/*** * 这个方法打包位jar文件就无法获取项目路径了。 * @return */
public static String getRealPath() {String realPath = PathUtil.class.getClassLoader().getResource("").getFile();java.io.File file = new java.io.File(realPath);realPath = file.getAbsolutePath();//去掉了最前面的斜杠/
try {realPath = java.net.URLDecoder.decode(realPath, "utf-8");
} catch (Exception e)
{e.printStackTrace();}return realPath;}
public static void main(String[] args) {System.out.println(projectPath);}}

热心网友

xx.class.getProtectionDomain().getCodeSource().getLocation().getPath();
获取 xx这个类文件的绝对路径去看看吧 剩下的就自己取解析吧
request.getContextPath(); 能直接获取

热心网友

String path = "D:/workspace/TEST";
String name = path.substring(path.LastIndexOf ('/'),path.length());

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com