Search code examples
javastringjardirectorytransport

Accessing variables from another Java program


I am making a Java program that switches between two jars using

Runtime.getRuntime().exec("cmd /c java -jar fileTwo.jar");

But, I need the class from one jar to somehow transport the string values within it to the class in the other jar. Since the two classes are from different jar files, I can't find any methods that can transport string between two jar directories.

If it helps, I am making a program that allows you to create and access accounts. That is why I need to transport strings, so I can store them in a data class to use as a reference for later logins. I have to use string because char can not be assigned the value gotten from the method jtfFirst.getText(); which is a JTextField.


Solution

  • Let one program write the string into a file and the other read it from the file. You can also pass the string as the command-line parameter to the second program. Say, s is the variable with the string. Then you can exec "cmd /c java -jar fileTwo.jar \""+s+"\"". (Enclose the string into quotation marks if it consists of more than one word). The string will be available to the second program as args[1].