我知道那里有很多相关的问题,但是我似乎被困在一些应该微不足道的事情上。我在 Wavemaker 中编写了一个 Java 服务,单击按钮即可将 2 个字符串参数(绑定(bind)到前端的编辑器)传递到 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”绑定(bind)到 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”中。我发现很奇怪的是,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”,一切都会很好地工作。我已经尝试了我能想到的参数周围的双引号和单引号的每一种组合,但可惜的是,通过按下Wavemaker前端上的按钮,java代码的输出不会被写入到这个测试文件中。 我知道这是一个有点冗长的问题,但如果有人对为什么这不起作用有任何见解,我将不胜感激。我确信我只是忽略了一些愚蠢的事情。 提前致谢。
最佳答案
去掉shell脚本中的java编译行后; javac pgQuery.java
参数传递下来没有问题。我不太明白为什么会这样,只是碰巧我碰巧在脚本中注释掉了该行并运行它。我想一旦java代码保持不变,就不需要每次执行脚本时都进行编译,但是我仍然不明白为什么每次编译都会阻止参数的传递。不管怎样,我想我应该发布这个,以防它对任何人有用。
关于java - 将参数从 shell 脚本传递到 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27988562/