我知道那里有很多相关的问题,但是我似乎被困在应该是微不足道的事情上。我在 Wavemaker 中编写了一个 java 服务,单击按钮将 2 个字符串参数(绑定到前端的编辑器)传递到我们 Linux 机器上的 shell 脚本中。我在 java 服务中使用进程构建器来访问 shell 脚本。代码如下。
package com.wavemeker;
import com.wavemaker.runtime.javaservice.JavaServiceSuperClass;
import com.wavemaker.runtime.service.annotations.ExposeToClient;
import java.io.IOException;
import java.io.File;
/*
* This is a client-facing service class. All
* public methods will be exposed to the client. Their return
* values and parameters will be passed to the client or taken
* from the client, respectively. This will be a singleton
* instance, shared between all requests.
* To log, call the superclass method log(LOG_LEVEL, String) or log(LOG_LEVEL, String, Exception).
* LOG_LEVEL is one of FATAL, ERROR, WARN, INFO and DEBUG to modify your log level.
* For info on these levels, look for tomcat/log4j documentation
*/
@ExposeToClient
public class xmlGen extends JavaServiceSuperClass {
public void readScript(String a, String b) throws IOException, InterruptedException
{
final File dir = new File("/var/lib/tomcat7/webapps/sh/");
final String shellScript = "./master_script.sh";
ProcessBuilder pb = new ProcessBuilder(shellScript, a, b);
pb.directory(dir);
System.out.println("Executing script...");
Process proc = pb.start();
try {
int shellExitStatus = proc.waitFor();
if(shellExitStatus != 0)
{
System.out.println("Success!!");
}
System.out.println("Script has completed successfully.");
}
catch (InterruptedException e)
{
System.out.println("Shell Script process was interrupted and did not complete.");
System.exit(1);
}
} // end method
} // end class
这里两个输入参数“a”和“b”绑定到 Wavemaker 服务变量,所以如果我在前面的屏幕上输入“十”和“二”,这些就是我传递给我的 shell 脚本的参数。他们实际上没有问题地传递下去,并且“master_script.sh”执行。我遇到的问题是这个脚本调用了一些其他脚本,这些脚本反过来调用较低级别的java代码也依赖于这两个字符串参数.. . master_script.sh 看起来像这样
#!/bin/bash
set -e
./script1.sh "$1" "$2"
./script2.sh "$1" "$2"
.
.
.
并给出这些脚本之一的示例;例如 script1.sh,看起来像这样
#!/bin/bash
set -e
FILEPATH1=/var/lib/tomcat7/webapps/data/test.txt
JPATH1=/var/lib/tomcat7/webapps/java/XML/src
> $FILEPATH1 # empties file contents before writing to it...
echo "testing params $1 and $2" > $FILEPATH1
cd $JPATH1
javac pgQuery.java
java -classpath postgresql-9.3-1102.jdbc41.jar:. pgQuery "$1" "$2" >> $FILEPATH1
所以最终我希望将java类的输出附加到文件“test.txt”中。我觉得奇怪的是 echo 工作正常并将我从 Wavemaker 输入的参数输出到测试文件,但是 java 似乎没有写出任何东西。为了让事情更清楚,这段 java 通过准备好的语句访问 postgresql 数据库,并以 xml 格式 (query_to_xml) 给出查询结果。为了完整起见,代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
public class pgQuery
{ // Begin Class
// JDBC driver name and db URL
static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://path...";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public void xml(int a, int b) { // Begin Main Method
Connection conn = null;
Statement stmt = null;
try //***START TRY BLOCK****//
{
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
//System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
String sql0 = "select query_to_xml('select cdt as CreationDateAndTime from table_name where x ='||?||' and y = '||?||'', true,true,'');";
PreparedStatement ps0 = conn.prepareStatement(sql0);
ps0.setInt(1, a);
ps0.setInt(2, b);
ResultSet rs0 = ps0.executeQuery();
ResultSetMetaData rsmd = rs0.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs0.next())
{
for(int i=1;i<=numberOfColumns;i++)
{
System.out.println(rs0.getString(i)+ " ");
}
}
ps0.close();
rs0.close();
conn.close();
} //****END TRY BLOCK*
catch(SQLException se){
//Handle errors for JDBC
System.out.println("Error with JDBC Connection!!");
se.printStackTrace();
}
catch(Exception e){
//Handle errors for Class.forName
System.out.println("Error with Class.forName... Driver Related!!");
e.printStackTrace();
}
finally{
//finally block to close resources
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}//end finally try
}//end try
}//end method
public static void main(String[] args)
{
int a1 = Integer.parseInt(args[0]);;
int b1 = Integer.parseInt(args[1]);;
pgQuery obj1 = new pgQuery();
obj1.xml(a1, b1);
} // end main method
}//end Class
如果我直接从 linux 执行 "master_script.sh "$1" "$2" 一切都很好。我已经尝试了我能想到的参数周围的双引号和单引号的所有组合,但是唉,java代码的输出不是通过按下 Wavemaker 前端的按钮将其写入此测试文件。我知道这是一个冗长的问题,但如果有人对为什么这不起作用有任何见解,我将不胜感激。我确定我只是忽略了一些愚蠢的事情。在此先感谢。