Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从bash脚本调用java进程时维护java对象状态_Java_Bash - Fatal编程技术网

从bash脚本调用java进程时维护java对象状态

从bash脚本调用java进程时维护java对象状态,java,bash,Java,Bash,我从bash脚本调用上面的java函数 public class Go { private static SomeObject fld; public static void main(String[] args) { //depending on the args I do call different methods(among //them one arg

我从bash脚本调用上面的java函数

    public class Go {
            private static SomeObject fld;
            public static void main(String[] args) {
                   //depending on the args I do call different methods(among 
                   //them one arg will do the job of initialization of fld variable)
            }
    }
为true时
做
读取命令
java-cp some-jar.jar Go
然后
假设command1在Go类中初始化fld,command2在初始化字段上进行一些处理

当我为不同的命令调用不同的java进程时,对象fld状态不会为下一个命令持久化


如何修改代码,使fld信息在不使用数据库或反序列化和序列化的情况下为下一个命令保留?

可以将结果存储在bash变量中:

Go.java

while true
do
    read commands
    java -cp some-jar.jar Go <commands>
then
VALUE=test

while true
do
    VALUE=`java  Go $VALUE`
    echo $VALUE
done
test4
test45
test456
test4567
test45678
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Go {
    private static String fld;

    public static void main(String[] args) throws IOException {
        fld = args[0];
        fld += fld.length();
        FileWriter fileWriter = new FileWriter("store.txt");
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(fld);
        printWriter.close();
    }
}
VALUE=store
while true
do

    java  Go $VALUE
    VALUE=`cat ./store.txt`
    echo $VALUE
done
run.sh

class Go {
    private static String fld;

    public static void main(String[] args) {
        fld = args[0];
        fld += fld.length();
        System.out.println(fld);
    }
}
输出

while true
do
    read commands
    java -cp some-jar.jar Go <commands>
then
VALUE=test

while true
do
    VALUE=`java  Go $VALUE`
    echo $VALUE
done
test4
test45
test456
test4567
test45678
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Go {
    private static String fld;

    public static void main(String[] args) throws IOException {
        fld = args[0];
        fld += fld.length();
        FileWriter fileWriter = new FileWriter("store.txt");
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(fld);
        printWriter.close();
    }
}
VALUE=store
while true
do

    java  Go $VALUE
    VALUE=`cat ./store.txt`
    echo $VALUE
done
或者将结果存储到文件中,在bash中读取并作为参数传递到Go.java中

以下是文件存储示例:

文件示例Go.java

while true
do
    read commands
    java -cp some-jar.jar Go <commands>
then
VALUE=test

while true
do
    VALUE=`java  Go $VALUE`
    echo $VALUE
done
test4
test45
test456
test4567
test45678
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Go {
    private static String fld;

    public static void main(String[] args) throws IOException {
        fld = args[0];
        fld += fld.length();
        FileWriter fileWriter = new FileWriter("store.txt");
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(fld);
        printWriter.close();
    }
}
VALUE=store
while true
do

    java  Go $VALUE
    VALUE=`cat ./store.txt`
    echo $VALUE
done
文件示例run.sh

while true
do
    read commands
    java -cp some-jar.jar Go <commands>
then
VALUE=test

while true
do
    VALUE=`java  Go $VALUE`
    echo $VALUE
done
test4
test45
test456
test4567
test45678
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Go {
    private static String fld;

    public static void main(String[] args) throws IOException {
        fld = args[0];
        fld += fld.length();
        FileWriter fileWriter = new FileWriter("store.txt");
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(fld);
        printWriter.close();
    }
}
VALUE=store
while true
do

    java  Go $VALUE
    VALUE=`cat ./store.txt`
    echo $VALUE
done
文件示例输出

while true
do
    read commands
    java -cp some-jar.jar Go <commands>
then
VALUE=test

while true
do
    VALUE=`java  Go $VALUE`
    echo $VALUE
done
test4
test45
test456
test4567
test45678
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

class Go {
    private static String fld;

    public static void main(String[] args) throws IOException {
        fld = args[0];
        fld += fld.length();
        FileWriter fileWriter = new FileWriter("store.txt");
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.print(fld);
        printWriter.close();
    }
}
VALUE=store
while true
do

    java  Go $VALUE
    VALUE=`cat ./store.txt`
    echo $VALUE
done

对java的每次调用都是一张白板——除非您在退出Main之前将状态存储到文件或其他东西中。您是否考虑过使用bash迭代所有命令,构建单个(大型)命令列表,然后将这些命令传递给java一次?或者,刚开始用Java阅读
命令
?您的意思是不要使用实际提供的用于持久化对象状态的功能。为什么要查找序列化和反序列化以外的其他替代方法?我需要对每个命令给出响应。在这里,我从交互式shell获取输入。有没有一种方法可以让第一个java进程保持活动状态,就像让bash进程保持活动状态一样@如果
fld
是字符串,则vinayprajapatit有效。如果代码< > FLD是一些java类的实例,我将考虑两种方法:1)Jackson <代码> ObjutsMatter < /C> >序列化到JSON 2)如果这个类实现了SerialDealEngy,则通过<代码> ObjutOutsStruts和 Base64 .GETEnCODE().EnCODotoString 将其序列化为Base64字符串。如果您需要,我可以创建一个示例。