将参数从 shell 脚本传递到 java 类



我知道那里有很多相关的问题,但是我似乎被困在应该微不足道的事情上。我在 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 前端上的按钮写入此测试文件。我知道这是一个有点冗长的问题,但如果有人对为什么这不起作用有任何见解,我将不胜感激。我敢肯定我只是忽略了一些愚蠢的东西。提前谢谢。

删除 shell 脚本中的 java 编译行后; javac pgQuery.java传递下来的参数没有问题。我不是特别明白为什么会这样,只是偶然地,我碰巧在脚本中注释掉了那行并运行了它。我想一旦 java 代码保持不变,它就不需要在每次执行脚本时进行编译,但是我仍然不明白为什么每次编译都会阻止参数的传递。无论哪种方式,我想我会发布这个,以防它被证明对任何人都有用。

相关内容

  • 没有找到相关文章

最新更新