package com.jason.test;

import java.io.File;
import java.io.IOException;
import java.net.URL; public class MyUrlDemo {
public static void main(String[] args) {
MyUrlDemo muDemo = new MyUrlDemo();
try {
muDemo.showURL();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void showURL() throws IOException { // 第一种:获取类加载的根路径 D:\git\daotie\daotie\target\classes
File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f); //C:\notos\code\sailertest\target\classes // 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录 D:\git\daotie\daotie\target\classes\my
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);//C:\notos\code\sailertest\target\classes\com\jason\test // 第二种:获取项目路径 D:\git\daotie\daotie
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);//C:\notos\code\sailertest // 第三种: file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
System.out.println(xmlpath);//file:/C:/notos/code/sailertest/target/classes/ // 第四种: D:\git\daotie\daotie
System.out.println(System.getProperty("user.dir"));//C:\notos\code\sailertest
/*
* 结果: C:\Documents and Settings\Administrator\workspace\projectName
* 获取当前工程路径
*/ // 第五种: 获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));//C:\Users\MI\AppData\Local\Temp\classpath.jar;C:\notos\software\idea\idea201703\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar } }

java 获取类路径的更多相关文章

  1. java获取类路径下文件的绝对路径

    获取文件绝对路径 在idea中,默认的当前路径是project的根路径,如果你使用idea的默认路径,只要离开idea换到其他位置,可能当前路径就不是project的根路径了. 使用一下通用方式的前提 ...

  2. Java获取类路径的方式

    Java环境中,如何获取当前类的路径.如何获取项目根路径等: @Test public void showURL() throws IOException { // 第一种:获取类加载的根路径 Fil ...

  3. java获取类路径

    String file = MessageTask3.class.getResource("").getFile(); File: public static final Stri ...

  4. 获取类路径中含有beans.xml的jar包名称

    获取类路径中含有beans.xml的jar包名称 package com.stono; import java.io.File; import java.io.IOException; import ...

  5. Javaweb学习笔记——(九)——————Servlet的进入,及ServletConfig、GenericServlet、HttpServlet、ServletContext、获取类路径资源

    Servlet1.什么是Servlet? *Servlet是Javaweb三大组件之一(Servlet,Filter,Listener) *Servlet是用来处理客户端请求的动态资源 *Servle ...

  6. java获取项目路径,url路径

    我的web项目名iamgeModel. 工作空间在D盘 先获取url相关: 需要是HttpServletRequest request; 获取IP: request.getServerName() / ...

  7. JavaWeb学习——获取类路径下的资源

    对于JavaWeb而言,获取类路径下的资源,就是获取classes目录下的资源. 获取资源的方式有两种,利用Class或ClassLoader. Class类的getResourceAsStream( ...

  8. JAVA获取CLASSPATH路径

    ClassLoader 提供了两个方法用于从装载的类路径中取得资源: public URL getResource (String name); public InputStream getResou ...

  9. JAVA获取CLASSPATH路径--转

    ClassLoader提供了两个方法用于从装载的类路径中取得资源: public URL getResource(String name);         public InputStream ge ...

随机推荐

  1. java DES转C#DES加密解密

    一个程序用到java的cn.core.jar加密的,需要在.NET 中解密,发现JAVA的des算法与C#的有点区别. 自己不太懂加密解密算法,所以找了个省事的方法,用IKVM.NET,用这个将cn. ...

  2. 解决:The web application [] registered the JDBC driver [] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

    问题描述 在将Spring Boot程序打包生成的war包部署到Tomcat后,启动Tomcat时总是报错,但是直接在IDEA中启动Application或者用"java -jar" ...

  3. 实体类id的几种生成方式

    @Id// @GeneratedValue(strategy = GenerationType.AUTO) // 自增// @GeneratedValue(strategy = GenerationT ...

  4. 26.Apache Solr RCE

    多事之秋,刚爆出来shiro的RCE,紧接着solr服务器就出了RCE 自从漏洞爆出来已经一段时间,复现漏洞多如牛毛,我这里来水一篇 漏洞简介 什么是solr服务器? Solr是一个独立的企业级搜索应 ...

  5. Python3的map/reduce

    Python内建了map()和reduce()函数. 原文在这里MapReduce: Simplified Data Processing on Large Clusters,map/reduce的概 ...

  6. 更改用户host留下的坑

    前言:  我们在创建数据库用户的时候都会指定host,即一个完整的用户可描述为 'username'@'host' .创建用户时不显式指定host则默认为%,%代表所有ip段都可以使用这个用户,我们也 ...

  7. Alipay支付宝调用错误:Call to undefined function openssl_sign()

    打开php.ini,找到这一行 ;extension=php_openssl.dll,将前面的“;”去掉: 重启服务器.

  8. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  9. vs2017 curl7.6编译

    nmake /f Makefile.vc mode=static VC=15 MACHINE=x86 nmake /f Makefile.vc mode=dll VC=15 MACHINE=x86 c ...

  10. nginx secure_link_module 访问包含

    server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.acce ...