Results 1 to 20 of 22
Thread: call c++ exe from java applet
- 07-27-2010, 07:54 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
call c++ exe from java applet
Hi ,
I am trying to call c++ exe from java applet.For this purpose am using processbuilder.My code is as follows
String line = "";
String s = "C:\\Users\\madsaan\\Documents\\NetBeansProjects\\ CppApplication_2\\dist\\Debug\\Cygwin-Windows\\cppapplication_2.exe";
ProcessBuilder pb = new ProcessBuilder(s);
Process process = pb.start();
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
JOptionPane.showMessageDialog(Cytoscape.getDesktop (), line);
}
The c++ exe looks like this when started:
1. Find the shortest path from s to t.
2. Find the associating path from s to t within length l.
3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
4. Exit
Please input the command (1-4):
once u input the command ,for example 1 we get
Please input s and t, separated by space:
so how am i supposed to pass arguments line by line .
I tried ProcessBuilder pb = new ProcessBuilder(s,"1","0 1");but all it does is print the following
1. Find the shortest path from s to t.
2. Find the associating path from s to t within length l.
3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
4. Exit
how can i get it to accept all the arguments and give me the final output alone?
Thanks.
- 07-27-2010, 09:45 PM #2
To communicate with a process, you'd need to use the Streams available from the Process. Read from one and write to the other.how can i get it to accept all the arguments and give me the final output alone?
If the program you are executing only gets its data from prompts and reads, then you have to read its prompts and write to its reads.
Or is this an OS piping (> or <) thing where you can "prefeed" the reads by putting the answers in a file that the OS uses to respond to reads.Last edited by Norm; 07-27-2010 at 09:48 PM.
- 07-28-2010, 07:50 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Hi,
Thanks for your reply.
This is not a OS piping.I managed to get part of it working using input and outputstream
The code is as follows
ProcessBuilder pb = new ProcessBuilder(s);
Process process = pb.start();
final InputStream is = process.getInputStream();
OutputStream out = process.getOutputStream();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
pw.println(1);
new Thread(new Runnable() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
}
}
}).start();
pw.close();
Right now am able to pass only the first argument.Am not able to pass the second set of arguments when it asks for
Please input s and t, separated by space:
Help!
Thanks
- 07-28-2010, 08:36 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Just imagine you, innocently clicking a URL link; a HTML page loads with an applet; also just imagine that the ms windows command "format'' were written in C++. Your applet is able to start that program and it formats your entire hard disk. Would you be happy?
kind regards,
Jos
- 07-28-2010, 10:01 PM #5
Are you talking about arguments to the program? Arguments go on the command line following the command name.able to pass only the first argument.Am not able to pass the second set of arguments
Or are you talking about responding to console prompts from the program?
To figure out the logic and calling sequences, can you build a small application and use it to work out the techniques?
You start the program in a Process and read its "prompt to the console" via an input stream
You write a response "to the its input console" via an output stream
Then read the next
and write the next
etcLast edited by Norm; 07-28-2010 at 10:04 PM.
- 07-28-2010, 10:07 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
I tried that as follows
try {
String s = "C:\\Users\\madsaan\\Documents\\NetBeansProjects\\ CppApplication_2\\dist\\Debug\\Cygwin-Windows\\cppapplication_2.exe";
ProcessBuilder pb = new ProcessBuilder(s);
Process process = pb.start();
OutputStream out = process.getOutputStream();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
pw.println(1);
OutputStream out1 = process.getOutputStream();
PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
pw1.println(1 + " " + 2);
pw1.close();
final InputStream is = process.getInputStream();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
}
}
}).start();
} catch (IOException d) {
d.printStackTrace();
}
}
}
it goes into an infinite loop .please help!.
- 07-28-2010, 10:11 PM #7
Please use code tags when you post your code to preserve formatting.
What does the program printout while in the loop?it goes into an infinite loop
- 07-28-2010, 10:12 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
am responding to the console prompts
- 07-28-2010, 10:12 PM #9
Can you explain your last post?
- 07-28-2010, 10:16 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Hi,i changed the code a little bit.(as follows)
ProcessBuilder pb = new ProcessBuilder(s);
Process process = pb.start();
OutputStream out = process.getOutputStream();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
pw.println(1);
pw.close();
OutputStream out1 = process.getOutputStream();
PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
pw1.println(1 + " " + 2);
pw1.close();
final InputStream is = process.getInputStream();
new Thread(new Runnable() {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
}
}
}).start();
Now it does not go into an infinite loop but it prints only the following
1. Find the shortest path from s to t.
2. Find the associating path from s to t within length l.
3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
4. Exit
Please input the command (1-4): Please input s and t, separated by space:
So,that means it taking in the first input(pw.println(1)) but after that if i run the program again it prints the same and does not take the next input (pw.println(1 + " " +2)).if it takes the second input then i will get the final output.Thanks for ur help.
- 07-28-2010, 10:27 PM #11
Please use code tags when you post your code to preserve formatting.
- 07-28-2010, 10:32 PM #12
What happens if you don't pre write the answers?
Read the prompt
Write an answer
Read another prompt
Write another answer
etc
- 07-28-2010, 10:40 PM #13
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Hi,
Am not pre writing the answers.I read one prompt,print the input,read the second prompt and print the input.it works for the first prompt but not for the second.I have pasted the formatted code as below
Java Code:ProcessBuilder pb = new ProcessBuilder(s); Process process = pb.start(); OutputStream out = process.getOutputStream(); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out))); pw.println(1); pw.close(); OutputStream out1 = process.getOutputStream(); PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1))); pw1.println(1 + " " + 2); pw1.close(); final InputStream is = process.getInputStream(); new Thread(new Runnable() { public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (java.io.IOException e) { } } }).start(); } catch (IOException d) { d.printStackTrace(); } } } }Last edited by jojji; 07-28-2010 at 10:53 PM.
- 07-28-2010, 10:40 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
sorry.am not sure how to use code tags in this forum.
-
- 07-28-2010, 10:51 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
great.Thanks
- 07-28-2010, 11:00 PM #17
It looks to me that you are writing the answer BEFORE (pre) reading the promptsAm not pre writing the answers.
Java Code:pw.println(1); // prefeed answer 1 pw.close(); OutputStream out1 = process.getOutputStream(); PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1))); pw1.println(1 + " " + 2); // prefeed answer 2
- 07-28-2010, 11:09 PM #18
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
I havent clearly understood what outputstream and inputstream exactly do.its very confusing actually.how do u think i should structure the code.please help
- 07-28-2010, 11:14 PM #19
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Hi,
I changed my code but am still getting the same result as before
Java Code:ProcessBuilder pb = new ProcessBuilder(s); Process process = pb.start(); OutputStream out = process.getOutputStream(); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out))); OutputStream out1 = process.getOutputStream(); PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1))); final InputStream is = process.getInputStream(); new Thread(new Runnable() { public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (java.io.IOException e) { } } }).start(); pw.println(1); pw.close(); pw1.println(1 + " " + 2); pw1.close(); } catch (IOException d) { d.printStackTrace(); } } } }
- 07-28-2010, 11:22 PM #20
Similar Threads
-
how to call Java Bean Ireport frm Java application
By Prashant.surwade in forum Advanced JavaReplies: 8Last Post: 05-06-2012, 03:39 PM -
How to call php script using Java
By narik in forum New To JavaReplies: 2Last Post: 01-22-2010, 05:08 PM -
How to Call a java service
By omikeneil in forum Advanced JavaReplies: 3Last Post: 10-17-2009, 03:30 AM -
how to call dll from java ??
By Omarero in forum New To JavaReplies: 3Last Post: 11-13-2008, 05:14 AM -
how to call a dll from java
By katie in forum Advanced JavaReplies: 3Last Post: 12-10-2007, 10:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks