Java 仅当程序从Runtime.exec()运行时发生SQL异常
我在理解以下问题时遇到一些问题。 出于我在这里不会(也不能)说明的原因,我必须编写一个外部JAR,在Java web应用程序内部执行,使用绑定到Runtime.exec()方法调用的进程。这个外部JAR在数据库中执行查询,并使用ApachePOI库在Excel文件中打印其结果。 代码如下:Java 仅当程序从Runtime.exec()运行时发生SQL异常,java,sql-server,exec,sqlexception,Java,Sql Server,Exec,Sqlexception,我在理解以下问题时遇到一些问题。 出于我在这里不会(也不能)说明的原因,我必须编写一个外部JAR,在Java web应用程序内部执行,使用绑定到Runtime.exec()方法调用的进程。这个外部JAR在数据库中执行查询,并使用ApachePOI库在Excel文件中打印其结果。 代码如下: public static void main(String[] args) { //Initializing error variable boolean error = false;
public static void main(String[] args) {
//Initializing error variable
boolean error = false;
//Initializing rownum variable
int rownum = 0;
//Initializing db connection parameters
String userName = args[0];
String password = args[1];
String dbUrl = args[2];
//Initializing file path
String filePath = args[3];
//Initializing query string
String query = args[4];
if(!query.endsWith(";"))
query += ";";
//Keep 100 rows in memory, exceeding rows will be flushed to disk
SXSSFWorkbook wb = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE);
//Temp files will be gzipped
wb.setCompressTempFiles(true);
//Initializing xlsx sheet
Sheet sh = wb.createSheet();
wb.setSheetName(wb.getSheetIndex(sh), "DBExtraction");
//Trying to connect to db and execute a query.
//If the previous operation succeeded, parse query results
//into an xlsx workbook
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(dbUrl, userName, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int cols = md.getColumnCount();
int cn = 0;
Row header = sh.createRow(rownum++);
for(int i = 0; i < cols; ++i) {
header.createCell(cn++).setCellValue(md.getColumnLabel(i + 1));
}
while(rs.next()) {
Row row = sh.createRow(rownum++);
cn = 0;
for (int cellnum = 0; cellnum < cols; cellnum++) {
Cell cell = row.createCell(cellnum);
cell.setCellValue(rs.getString(++cn));
}
}
for(int j = 0; j < cols; ++j) {
sh.setColumnWidth(j, 67*256);
}
FileOutputStream fos = new FileOutputStream(filePath);
//Writing workbook to file
wb.write(fos);
//Closing file
fos.flush();
fos.close();
//Dispose of temporary files backing this workbook on disk
wb.dispose();
wb.close();
//Closing db connection objects
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
error = true;
e.printStackTrace();
}
if(error)
System.exit(1);
else
System.exit(0);
}
在第二种情况下,我得到以下异常:
java.sql.SQLException:找不到适合“jdbc:sqlserver://remoteServer:1433;databaseName=dbName“
web应用程序当前正在CentOS7服务器上的Wildfly 10.0.0-Final上运行
谢谢你抽出时间
编辑:
我认为添加JAR清单是个好主意,因为它可能包含一些错误(JAR文件是由Eclipse导出生成的):
另外,这是JAR的内部结构:。我最终设法解决了这个问题,但效率非常低(而且非常难看) 我必须创建一个shell脚本来解析外部JAR参数,因为在转换调用JAR和实际终端的web应用程序方法中的引号(“)字符时出现了一些问题
我不打算提供shell脚本源代码(除非明确要求),因为我有点为自己感到羞耻。不是关于代码本身,而是关于这个“解决方法”。@esprittn第一个问题:“出于我不会(也不能)在这里说的原因”…@esprittn-Erm,我不是OP。请注意阅读问题:当我试图直接从终端(cmd或bash)执行程序时。。。“您是否尝试(如果可以)使用java-cp而不是java-jar?这样您就可以指定类路径并确保jdbc驱动程序在其中。语法看起来像java-cp”[your classpath]“[your.java.main]args…”@RobertoBenazzato我还没有尝试您的解决方案,但我会尽快尝试(不幸的是,这并不完全取决于我)@RobertoBenazzato我尝试从Runtime.exec()执行以下命令:
java-cp executable.jar:/path/to/driver/sqljdbc.jar com.company.package.MainClass“user”“pwd”“jdbc:sqlserver://remoteServer:1433;databaseName=dbName“路径”查询。
不幸的是,我得到了相同的SQLException。
Process p = null;
p = Runtime.getRuntime().exec("java -jar " +
jarDir +
File.separator +
"executable.jar " +
"\"" + param0 + "\" " +
"\"" + param1 + "\" " +
"\"" + param2 + "\" " +
"\"" + param3 + "\" " +
"\"" + param4 +"\"");
p.waitFor();
String line;
BufferedReader error = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
while((line = error.readLine()) != null){
System.out.println(line);
}
error.close();
BufferedReader input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
System.out.println("EXIT VALUE: " + p.exitValue());