当通过java -jar或者命令行mvn spring-boot:run的方式启动springboot项目时,会引导执行如下代码:

public static void main(String[] args) throws Exception {
	new JarLauncher().launch(args);

调用JarLauncher不带参数的构造方法创建JarLauncher对象,根据Java的继承特性会调用父类中的createArchive方法,官方代码实现如下:

protected final Archive createArchive() throws Exception {
	ProtectionDomain protectionDomain = getClass().getProtectionDomain();
	CodeSource codeSource = protectionDomain.getCodeSource();
	URI location = (codeSource != null) ? codeSource.getLocation().toURI() : null;
	String path = (location != null) ? location.getSchemeSpecificPart() : null;
	if (path == null) {
		throw new IllegalStateException("Unable to determine code source archive");
	File root = new File(path);
	if (!root.exists()) {
		throw new IllegalStateException("Unable to determine code source archive from " + root);
	return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));

整个方法实现思路较为简单,即获取当前类所在的某个路径(后面讲解)下是否存在某个文件,然后根据是文件还是文件夹构建不同对象。那么这个路径是什么呢?于是把代码做了简单处理打印path,做了如下四个测试,得出以下现象:

  1. 把代码放置到测试类中执行时,输出:/D:/SoftDevelop/idea_workspace/first-app-by-gui/target/test-classes/
  2. 把代码放置到springboot启动类时,输出:/D:/SoftDevelop/idea_workspace/first-app-by-gui/target/classes/
  3. 打包成jar包,通过地址栏访问/sayPath时,输出:file:/C:/Users/Administrator/Desktop/first-app-by-gui-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
  4. 打包成jar包,作为第三方依赖包时,输出:file:/C:/Users/Administrator/Desktop/first-app-by-gui-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/first-app-by-gui-0.0.2-SNAPSHOT.jar!/

综上所述,可以得出getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getSchemeSpecificPart()返回的内容为当前类所在的根路径,这里的根路径需要根据上述几种情况结合实际分析!

以上,完了!!

getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getSchemeSpecificPart()返回内容解析 当通过java -jar或者命令行mvn spring-boot:run的方式启动springboot项目时,会引导执行如下代码:public static void main(String[] args) throws Exception { new JarLauncher().launch(args);}调用JarLauncher不带参数的构造方法创建JarLauncher对象,根据Java的继承特性会调用父类中的createArchive方法,官方代码实现如下:protected
图书管理系统数据库源代码 //创建工程及设计主界面 public class Main extends JFrame { private static final JDesktopPane { DESKTOP_PANE=new JDesktopPane(); //桌面窗体 } public static void main(String[] args) //入口方法 { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //设置系统界面外观 new BookLogin(); //登录窗口 } catch(Exception ex) { ex.printStackTrace(); } } public static void addIFame(JInternalFrame iframe) //添加子窗体的方法 { DESKTOP_PANE。add(iframe); //新增子窗体 } public Main() { super(); //设置"关闭"按钮处理事件 setDefaultCloseOperation(WindowConstants。EXIT_ON_CLOSE); //创建工具栏 Toolkit tool=Toolkit,getDefaultToolkit(); //获得屏幕大小 Dimension screenSize=tool.getScreenSize(); setSize(800,600); //设置窗体大小 setLocation((screenSize.width—getWidth())/2,(screenSize。height- getHeight())/2; //设置窗体位置 setTitle("图书管理系统"); //设置窗体标题 JMenuBar menuBar=createMenu(); //创建菜单栏 setJMenuBar(menuBar); //设置菜单栏 JToolBar toolBar=createToolBar(); //创建工具栏的方法 getContentPane(),add(toolBar,BorderLayout。NORTH); //设置工具栏 final JLable lable=new JLable(); //创建一个标签,用来显示图片 lable。setBounds(0,0,0,0); //设置窗体的大小和位置 lable。setIcon(null); //窗体背景 DESKTOP_PANE.addComponentListener(new ComponentAdapter()) { public void componentResized(final ComponentEvent e) { Dimension size=e。getComponent().getSize(); //获得组建大小 lable。setSize(e.getComponent().getSize()); //设置标签大小 lable.setText("〈html><img width="+size.width+"height="+size.height+"src='"+this.getClass().getResourc e("/backImg。jpg")+"'〉</html〉"); //设置标签文本,设置窗口背景 } } //将标签添加到桌面窗体 DESKTOP_PANE。add(lable,new Integer(Integer。MIN_VALUE)); getContentPane().add(DESKTOP_PANE); //将桌面窗体添加到主窗体中 } } private JToolBar createToolBar() //创建工具栏的方法 { JToolBar toolBar=new JToolBar(); //初始化工具栏 toolBar.setFloatable(false); //设置是否可以移动工具栏 toolBar.setBorder(new BevelBorder(BevelBorder。RAIZED)); //设置边框 //图书信息添加按钮 JButton bookAddButton=new JButton(MenuActions.BOOK_ADD); ImageIcon icon=new ImageIcon(Main.class.getResource("/bookAddtb.jpg")); //添加菜单栏图标 bookAddButton.setIcon(icon); //设置按钮图标 bookAddButton.setHideActionText(true); //显
java 根据类名示例化类 类class getProtectionDomain()方法 (Class class getProtectionDomain() method) getProtectionDomain() method is available in java.lang package. getProtectionDomain()方法在java.lang包中可用。 getProtec...
String path = App.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 如果有中文; path = java.net.URL... 记录一次svn报错:[Previous operation has not finished; run 'cleanup' if it was interrupted] 的排错过程 47276