从shell返回字符串到java文件

时间:2018-02-21 18:14:27

标签: java shell

我已经用下面的方式编写了代码。

Java程序1(调用shell程序)=> Shell程序=> Java程序2(将执行加密)。

因此,当我运行Java程序1时,它应该调用shell,然后调用java程序2. java程序2将把加密值返回给我,同样需要传递给Java程序1.

直接从命令提示符执行shell时,我收到如下信息。

[Serverapp01 AESEncryption]$ sh AESEncrypt.sh /keys/keyfile_05.txt texttoencrypt
AESEncrypt.sh: line 28: exit: 0UhSI9LTa/7efiT0quKUsg==: numeric argument required

我的目标是将值0UhSI9LTa/7efiT0quKUsg==添加到我的java程序1.但是我没有从shell到java程序1获取此值。

非常感谢任何帮助。

Java Program 1

package com.abc.encrypt;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class AESEncrypt1 {


    public static String callScript(String encryptionScriptPath,String KeyLocation,String textToEncrypt) 
    {
        int value = 0;
        String enc ="Error";
        try 
        {
            ProcessBuilder pb = new ProcessBuilder(encryptionScriptPath,KeyLocation,textToEncrypt);
            Process p = pb.start();
            value = p.waitFor();
            BufferedReader br=new BufferedReader(
                    new InputStreamReader(
                       p.getInputStream()));
                    String line;
                    String temp ="";
                    while((line=br.readLine())!=null){
                       System.out.println(line);
                       temp = temp + line;
                    }
                    enc=temp;
        } 
        catch (Exception e) 
        {
            //value = 2;
            enc = "ErrorOccured.."+e.toString();
        } 

            return enc;
    }

}

Shell如下所示

unset CLASSPATH
KeyLocation=$1
TextToEncrypt=$2
isolatedjreLocation=/AESEncryption/isolatedjre17
JarLocation=/AESEncryption
CLASSPATH="/AESEncryption/isolatedjre17/jre17/bin"
export CLASSPATH
#echo $CLASSPATH

encryptedValue='ErrorInJava'

#echo 'Call Java Program'
encryptedValue=$($isolatedjreLocation/jre17/bin/java -cp $JarLocation/AESCryptoJava_v1.0.0.jar com.abc.AESEncryption $KeyLocation $TextToEncrypt)

#echo $encryptedValue

exit $encryptedValue

0 个答案:

没有答案