String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(t);
int num=t.indexOf(".metadata");
System.out.println(num); //这里输出-1
String path=t.substring(1,num).replace('/', '\\')+"CBDParkingSystem\\WebContent\\img";
System.out.println(path);
下面是报错-----------》》》:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
at java.lang.String.substring(Unknown Source)
at com.lovo.dao.test.ExternalContractsTest.main(ExternalContractsTest.java:77)
/C:/Users/Administrator/Desktop/CBDParkingSystem/build/classes/
-1
没有这个文件.metadata,所以输出了-1,导至substring索引越界了……