Pass String as params from one Java App to another
accepted answer by jverd on OTN
Yes, there are other ways. Is this way not meeting your needs?
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"});
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.
You could have one process open a ServerSocket and the other connect to it and send the data that way.
You could use a higher-level messaging tool like JMS, Active MQ, etc.
You could use RMI.
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.
Passing parameter from one process to another in Java
Basically the idea is to bind a local port on start up, if fails connects to it and send parameters.
public class StartOnce {
public static void main(String args[]) throws IOException {
SocketAddress addr = new InetSocketAddress("127.0.0.1", 9999);
try {
ServerSocket ss = new ServerSocket();
// tries to bind to localhost:9999
ss.bind(addr);
// Ok, I'm the first instance, listen for other instances for update.
while(true) {
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
s.close();
}
} catch (BindException e) {
// BindException, tries to send message to the first instance.
System.out.println("Another instance is running. Say hi.");
Socket s = new Socket();
s.connect(addr);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("Hello.");
for (String arg : args) {
pw.println(arg);
}
pw.close();
s.close();
}
}
}
How to pass parameters in a request?
You are sending an Object in @Query
param, Only @Body
accepts the object as its param. What you want is to convert your Object into JsonObject. I assume you're using Gson Library.
NewOrderRequest newOrderRequest = new NewOrderRequest();
newOrderRequest.setPhone("+911");
NetworkService.getInstance()
.service()
.newOrder(new Gson().toJson(newOrderRequest))
How to pass a String value to another class in Java
There are several mistakes here.
First off, in your main class as you write you call the method survey()
on the CharacterSurvey
object but the survey itself the way it is implemented needs a String parameter to work
public void survey(String character)
Also this method returns void. If you want somehow to grab a string out of that method you need to declare the method as
public String survey() {}
this method returns a string now.
If i were to give a general idea, declare a String
variable in the second class which will be manipulated inside the survey method and once the survey is declared as a String
method return the value at the end inside the method.
By doing that you'll be able to receive the String value by calling the method on the characterSurvey
object (and of course assign the value to a string variable or use it however).
Hope this helped
How do you pass a string from one activity to another?
Pass values using intents.
In your first activity
Intent i= new Intent("com.example.secondActivity");
i.putExtra("key",mystring);
// for explicit intents
// Intent i= new Intent(ActivityName.this,SecondActivity.class);
// parameter 1 is the key
// parameter 2 is the value
// your value
startActivity(i);
In your second activity retrieve it.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}
To pass custom objects you can have a look at this link
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
Passing values from one java program to another
You can pass the int argument to your second Java program as follows:
String[] cmd = { s, Integer.toString(n) };
Process pro=Runtime.getRuntime().exec(cmd);
... or as a single String
:
Process pro=Runtime.getRuntime().exec(String.format("%s %d", s, n);
Related Topics
Spring Boot How to Hide Passwords in Properties File
Where Should I Put the Log4J.Properties File
Java Regex Capturing Groups Indexes
In What Order Do Static Blocks and Initialization Blocks Execute When Using Inheritance
How to Make a Java Class That Implements One Interface with Two Generic Types
How to Find Out the Currently Logged-In User in Spring Boot
Shared Memory Between Two Jvms
Spring Data JPA - "No Property Found for Type" Exception
Is There a Java Utility to Do a Deep Comparison of Two Objects
Flutter Doctor --Android-Licenses Gives a Java Error
Handling Soft-Deletes with Spring JPA
In Java, Is It More Efficient to Use Byte or Short Instead of Int and Float Instead of Double
Case Insensitive String as Hashmap Key
Format File Size as Mb, Gb, etc