我知道那里有很多相关的问题,但是我似乎陷入了一些应该是微不足道的事情。我在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服务变量,所以如果我在前面的屏幕上输入“ten”和“two”,这些是我传递给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”。我觉得很奇怪,回声工作正常并输出我从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前端的按钮,代码不会写入此测试文件。 我知道这是一个很长的问题,但如果有人知道为什么这不起作用我会非常感激。我敢肯定我只是在忽视一些愚蠢的事情。 提前谢谢。
答案 0 :(得分:0)
删除shell脚本中的java编译行后; javac pgQuery.java
传递的参数没有问题。我并不特别明白为什么会这样,而且只是碰巧我在脚本中注释掉了这一行并运行它。我想一旦java代码保持不变,每次执行脚本时都不需要编译,但是我仍然不知道为什么每次编译都会阻止传递参数。无论哪种方式,我想我会发布这个以防它对任何人都有用。