JavaPrj使用: System.getProperty("user.dir")+"\\src"或直接"\\src"
JavaWeb使用Thread.currentThread().getContextClassLoader().getResource("/").getPath()

获取src目录

JavaWeb

JavaProj

System.getProperty("user.dir")

D:\tools\MyEclipse2015

D:\tools\Workspaces\TestJava

Thread.currentThread()
.getContextClassLoader()
.getResource("/")
.getPath()

/D:/tools/Workspaces
/.metadata/.me_tcat7
/webapps/AhExam4SSM
/WEB-INF/classes/

java.lang.NullPointerException

2018-04-29补充:JavaWeb项目中:

String path = this.getServletContext().getRealPath("/WEB-INF/classes");
System.out.println("path = " + path); String path2 = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
System.out.println("path2 = " + path2);

运行结果:(path2中空格变成%)

path = C:\Users\AndyMi\Workspaces\MyEclipse 2015\.metadata\.me_tcat7\webapps\TestJavaWeb\WEB-INF\classes
path2 = /C:/Users/AndyMi/Workspaces/MyEclipse%202015/.metadata/.me_tcat7/webapps/TestJavaWeb/WEB-INF/classes/

  

JAVA获取运行程序的src路径的更多相关文章

  1. Windows/Linux获取当前运行程序的绝对路径

    windows 获取当前运行程序的绝对路径(.exe) GetModuleFileNameA()函数获取绝对路径,不过文件路径中的反斜杠需要进行替换. ]; GetModuleFileNameA(NU ...

  2. C#获取运行程序的进程ID

    C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...

  3. Winform获取当前程序名称或路径

    以下几种方法获取当前程序名称或路径: // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.      // 获 ...

  4. Java 获取小程序openid(基于SpringBoot)

    Java 获取小程序openid(基于SpringBoot) 官方文档 wx.login 1.引入Request封装依赖 <!--Request依赖--> <dependency&g ...

  5. Java获取运行环境信息

    在做视频截取封面的时候用到了ffmpeg.我采用的是通过Java调用bat或sh脚本然后生成图片文件. 在线上使用的是Centos 7.所以程序中需要获取到当前运行环境的信息来选择调用bat命令还是s ...

  6. JAVA 获取web文件的相对路径

    转自:http://wwwdd2315.blog.163.com/blog/static/66661889201091953350298/ 在JAVA文件中获取该项目的相对路径1.基本概念的理解 绝对 ...

  7. Linux中获取当前程序的绝对路径

    代码如下: char current_absolut_path[MAX_SIZE] = ""; memset(current_absolut_path,,MAX_SIZE); // ...

  8. 获取应用程序根目录物理路径(Web and Windows)

    这两个计划写一个小类库,需要在不同项目下任意调用.该类库需要对磁盘文件进行读写,所以就需要获取程序执行的磁盘路径,就简单的对获取磁盘路径的方法进行研究. 借助搜索引擎,我从网上搜罗来多种方法,都可以直 ...

  9. Linux下获取当前程序的绝对路径

    在Linux开发应用时,我们常常需要在程序中获取当前程序绝对路径,我们可以通过readlink读取符号链接/proc/self/exe进行获取,这个符号链接代表当前程序,它的源路径就是当前程序的绝对路 ...

随机推荐

  1. js利用sort()方法实现数组排序

    1.number类型排序 let aa = [1,11,2,4,3] aa.sort() console.log(aa) //[1,11,2,3,4] aa.sort((a,b)=>a-b) c ...

  2. layui布局器网站工具

    http://layuiout.magicalcoder.com/magicaldrag-admin/drag

  3. 解决Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/cqupt/paging/dao/User.xml

    搭建了一个ssm项目,启动报错Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find re ...

  4. Java多线程入门中几个常用的方法

    一.currentThread()方法 currentThread方法就是返回当前被调用的线程. 该方法为一个本地方法,原码如下: /** * Returns a reference to the c ...

  5. Xcode 和 VisualC++输出流的差别的理解

    将这样一段程序分别运行与Visual Studio 和 Xcode上边的结果: #include <iostream> using namespace std; int main() { ...

  6. 535种使用JavaScript重新加载页面的方法

    除了location = location之外还有534中方法重新加载页面 location = location location = location.href location = window ...

  7. mac出现zsh: command not found: ping解决方法

    Step1:终端输入以下命令: /sbin/ping 若出现如下信息,说明包含ping命令,是zsh的 PATH有问题,表示没有加载sbin下的命令,需要编辑.zshrc文件. Step2:终端打开. ...

  8. I think I need a boat house

    I think I need a boat house. Fred Mapper is considering purchasing some land in Louisiana to build h ...

  9. Concordion test

    reference documents http://concordion.org/Example.html

  10. BSGS模板(慢速)

    //author Eterna #define Hello the_cruel_world! #pragma GCC optimize(2) #include<iostream> #inc ...