开发者

Pass String as params from one Java App to another

开发者 https://www.devze.com 2023-03-08 17:11 出处:网络
I\'m trying to pass String as parameter from one Java Aplications to second as StartUp parameter for example I have Aplications that must call start another Java Aplication (just contains only JOptio

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

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,

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();
    }
} 

my question :

EDIT1: 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: my crosspost http://forums.oracle.com/forums/thread.jspa?threadID=2229798&tstart=0

accepted answer from OTN


accepted answer by jverd on OTN

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

  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", "Passed info");

// OR

exec(new String[] {"cmd", "/c", "start", "java", "-jar", "C:\\Dialog.jar", "Passed info"});
  1. You could put the information in a file that the second process reads.

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

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

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

  5. You could use RMI.

  6. You could use CORBA.

I'm sure there are other approaches as well.

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.


Dude,

Read When runtime exec won't and get back to us if you're still stuck.

It's a good article. I'm guessing you've got "a problem with your parameter" ;-)

Cheers. Keith.

0

精彩评论

暂无评论...
验证码 换一张
取 消