Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

executable jar - Pass String as params from one Java App to another

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...