Java中项目路径,就是classpath了。
本文结合IntelliJ IDEA分享一下对classpath的理解了
前期准备工作(包括相关工具或所使用的原料等)javaIntelliJ IDEA 详细的操作方法或具体步骤
项目路径,就是指classpath的根路径了。
是查找配置文件和classloader加载bytecode的起点
这次就以IntelliJ IDEA为例,来聊聊项目路径,也就是classpath的事
前面分享了一篇classpath和path的区别,有兴趣的tx可以去看看

使用Java API来查看。
Code:
public class ClassPathDemo {
public static void main(String[] args) {
String classPath=ClassPathDemo.class.getResource("/").getPath(); System.out.println("项目路径:" + classPath);
}
}

执行上述代码,看看打印的信息
Output:
项目路径:/E:/java/JavaStudy/out/production/JavaStudy/

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

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

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


- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
