我试图让我的.jar内的类包的目录为File
(层次是/controller/core
):是否可以在.jar中获取类包的目录?
File directory_file_of_package = new File(getClass().getResource("/controller/core").toURI());
,当我在IDE中运行的程序,但是当我运行它,其中一期工程作为一个独立的.jar,它给了我这个错误:
java.lang.IllegalArgumentException: URI is not hierarchical
综观这些问题(why my URI is not hierarchical?和Java Jar file: use resource errors: URI is not hierarchical)并不能帮助我,因为涉及这些问题获得的.jar作为资源内的File
(getResourceAsStream()
) ,但我想获得的是一个目录。
此外,公认的答案状态是:
Generally the IDEs separates on file system classes and resources. But when the jar is created they are put all together.
那么,有没有办法抢/controller/core
目录?
我的XY问题:
我试图读取使用CSVJDBC(http://csvjdbc.sourceforge.net/)一个.csv文件,该方式DriverManager
引用的CSV文件好像包含文件夹是数据库和CSV文件是表。
public class SearchData {
void SearchData() {
try {
Class.forName("org.relique.jdbc.csv.CsvDriver");
File directory_file_of_package_containing_csv = new File(getClass().getResource("/controller/core").toURI());
// Driver points to directory which contains the CSV file as if it is the database
Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + directory_file_of_package_containing_csv.toString());
System.out.println("Connection established.");
Statement stmt = conn.createStatement();
System.out.println("Statement created.");
// CSV is named "data.csv" so the CsvDriver sees "data" as the table name
String sql = "SELECT id,name FROM data";
ResultSet results = stmt.executeQuery(sql);
...
} catch (ClassNotFoundException | SQLException | URISyntaxException e) {
e.printStackTrace();
}
}
}
如何将CsvJdbc指向包含在.jar中的csv文件?