将String作为params从一个Java App传递到另一个Java App [英] Pass String as params from one Java App to another

查看:127
本文介绍了将String作为params从一个Java App传递到另一个Java App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将String作为参数从一个Java Aplications传递到第二个作为StartUp参数

I'm trying to pass String as parameter from one Java Aplications to second as StartUp parameter

例如我有应用程序必须调用start另一个Java Aplication(只是在 System.exit(0); 之前只包含JOptionPane,JDialog或简单的JFrame),我试图从关闭应用程序向另一个应用程序发送一些描述,

for example I have Aplications that must call start another Java Aplication (just contains only JOptionPane, JDialog or simple JFrame) before System.exit(0);, there I trying to send some descriptions from closing application to another,

这些代码是我试过的模拟,在这种形式下,代码正常工作并将字符串显示到JTextArea ...

these code is simulations what I tried that and in this form, code works correctly and displayed String into the JTextArea ...

    import java.io.IOException;
    import java.util.concurrent.*;

    public class TestScheduler {

        public static void main(String[] args) throws InterruptedException {
            ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
            executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
            executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
            for (int i = 0; i < 10; i++) {
                final int j = i;
                System.out.println("assign : " + i);
                ScheduledFuture<?> future = executor.schedule(new Runnable() {

                    @Override
                    public void run() {
                        System.out.println("run : " + j);
                    }
                }, 2, TimeUnit.SECONDS);
            }
            System.out.println("executor.shutdown() ....");
            executor.shutdown();
            executor.awaitTermination(10, TimeUnit.SECONDS);
            try {
                Process p = Runtime.getRuntime().exec("cmd /c start java -jar C:\\Dialog.jar 'Passed info'");
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            System.out.println("System.exit(0) .....");
            System.exit(0);
        }

        private TestScheduler() {
        }
    }

//
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

public class Main {

    private static ArrayList<String> list = new ArrayList<String>();

    public Main() {
        JFrame frm = new JFrame();
        JTextArea text = new JTextArea();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); ++i) {
                text.append(list.get(i));
            }
        }
        JScrollPane scroll = new JScrollPane(text,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.add(scroll, BorderLayout.CENTER);
        frm.setLocation(150, 100);
        frm.setSize(new Dimension(400, 300));
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        if (args.length > 0) {
            for (String s : args) {
                list.add(s);
                System.out.print(s + " ");
            }
        }
        Main m = new Main();
    }
} 

我的问题:

EDIT1:如果存在另一种方法如何将一个Java Aplication(必须称为System.exit(0);)传递给另一个Java Aplication,另一种方法是我尝试使用Process / ProcessBuilder

if is there exist another way how to pass some value from one Java Aplication (there must be called System.exit(0);) to another Java Aplication, another way as I tried by using Process/ProcessBuilder

EDIT2:我的crosspost http://forums.oracle.com/forums/thread.jspa?threadID=2229798&tstart=0

my crosspost http://forums.oracle.com/forums/thread.jspa?threadID=2229798&tstart=0

接受的答案来自OTN

accepted answer from OTN

推荐答案

jverd在OTN上接受的回答

accepted answer by jverd on OTN

是的,还有其他方法。这种方式不能满足你的需求吗?

Yes, there are other ways. Is this way not meeting your needs?


  1. 还有另一个带有数组的exec()签名,其中第一个元素是命令和其余元素是它的args。它可能是也可能不是varargs电话。这看起来像这样,虽然它可能与我没有完全相同。

  1. There's another exec() signature that takes an array, where the first element is the command and the rest of the elements are its args. It may or may not be a varargs call. That would look something like this, although it might not work exactly as I have it.

exec(cmd,/ c,start,java , - jar,C:\ Dialog.jar,通过信息);

exec("cmd", "/c", "start", "java", "-jar", "C:\Dialog.jar", "Passed info");

// OR

exec(new String[] {"cmd", "/c", "start", "java", "-jar", "C:\\Dialog.jar", "Passed info"});




  1. 您可以将信息放在第二个文件中流程读取。

  1. You could put the information in a file that the second process reads.

您可以将信息存储在第二个流程查询的数据库中。

You could store the information in a database that the second process queries.

您可以让一个进程打开一个ServerSocket,另一个进程连接到它并以这种方式发送数据。

You could have one process open a ServerSocket and the other connect to it and send the data that way.

您可以使用更高级别的消息像JMS,Active MQ等工具。

You could use a higher-level messaging tool like JMS, Active MQ, etc.

您可以使用RMI。

您可以使用CORBA。

You could use CORBA.

我确信还有其他方法。

我不知道哪种方法最适合您的需求。这是你需要弄清楚的,虽然如果你不能决定,如果你在这里发布关于你的要求的更多细节,有人可能会提供一些建议。

I have no idea which approach is best suited to your needs. That's something you'll need to figure out, although if you can't decide, if you post more details about your requirements here, somebody may offer some advice.

这篇关于将String作为params从一个Java App传递到另一个Java App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆