Java如何获取当前的jar包路径

	public LatticeAlgorithm(BinaryRelation bRel,RelationalContextEditor rce)
	{
		projectPath=this.getPath();//双击 && 右键打开方式Java...
		if(projectPath.equals(""))//cmd窗口: java -jar *.jar
			projectPath=System.getProperty("user.dir");
		String javaProject=System.getProperty("user.dir");
		//下面是javaproject和jar可执行文件的区别
		if(LatticeAlgorithm.class.getResource("LatticeAlgorithm.class").toString().startsWith("file"))
			projectPath=javaProject;//java工程中执行
		binRel = bRel;
		this.rce=rce;
		lattice = new CompleteConceptLatticeImp();
	}
	private String getPath()
	{
		String filePath = System.getProperty("java.class.path");
		String pathSplit = System.getProperty("path.separator");// windows下是";",linux下是":"

		if (filePath.contains(pathSplit))
		{
			filePath = filePath.substring(0, filePath.indexOf(pathSplit));
		} else if (filePath.endsWith(".jar"))
		{// 截取路径中的jar包名,可执行jar包运行的结果里包含".jar"

			// 此时的路径是"E:\workspace\Demorun\Demorun_fat.jar",用"/"分割不行
			// 下面的语句输出是-1,应该改为lastIndexOf("\\")或者lastIndexOf(File.separator)
			// System.out.println("getPath2:"+filePath.lastIndexOf("/"));
			filePath = filePath.substring(0, filePath.lastIndexOf(File.separator) + 1);

		}
		return filePath;
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值