
java如何获取项目路径?
java获取项目路径的方法:
1.项目路径,就是指classpath的根路径了。
是查找配置文件和classloader加载bytecode的起点
这次就以IntelliJ IDEA为例,来聊聊项目路径,也就是classpath的事
立即学习“Java免费学习笔记(深入)”;
前面分享了一篇classpath和path的区别,有兴趣的tx可以去看看

2.使用Java API来查看。
Code:
public class ClassPathDemo {
public static void main(String[] args) {
String classPath = ClassPathDemo.class.getResource("/").getPath();
System.out.println("项目路径:" + classPath);
}
}
3.执行上述代码,看看打印的信息
Output:
项目路径:/E:/java/JavaStudy/out/production/JavaStudy/

4.在使用IDEA的过程,通过API查看项目路径来找编译好的class比较麻烦。
这个完全可以在IDEA的配置中的找嘛
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
193
下面就分享下在IDEA配置中怎么查看项目路径,也就是找到执行代码的classpath
在Project 面板中点右键,在弹出的菜单中选“Open Module Settings”

5.在弹出的“Project Structure”对话框中,选中“Paths”Tab选项卡
在Compiler output中,默认选择的是“Inherit project compile output path”
也就是当前module使用的是Project的Compiler output路径。
那么Project的Compiler output路径在哪呢?

6.点左侧的“Project”选项,在右侧窗口可以看到Project的compiler output
这个路径是不是和JAVA API的输入基本是一致的呢。
“This directory contains tow subdirectories:Production and Test for production code and test sources,respectively.”


推荐教程:《JAVA视频教程》
以上就是java如何获取项目路径?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号