• Post Reply Bookmark Topic Watch Topic
  • New Topic

Execute c file using java  RSS feed

 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#include<stdio.h>
void main()
{
int a;
scanf("%d",a);
printf("%d",a);
}


I want give input and get output for this program.please help me
Advance Thanks for you
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just compile and link the C program and run it?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can call C code via the Java Native Interface (JNI) but that is by no means a simple topic. You can find tutorials about JNI easlily enough. As Bear says, you can simply compile that program as C code.
Moving discussion to a more appropriate forum.
 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir please reply code solution for this program
 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am already tried to compile and run.the compile was succesfully done but run time ,i am only use printf the program will execute corectly,when i use scanf the program process long time
 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my Program for Execute c program
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to a resource that might be of help -> Java Native Interface Tutorial
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be very careful when using Process as there are a number of gotcha's which you need to be aware of. Read this article very carefully.
 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String cmd="C:\\TC\\BIN\\sample.exe";
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd /c \"" + cmd + "\"");
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader ebr=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line=null;
while ((line=br.readLine())!=null) {
Txt2.append(line+"\n");
}
while ((line=ebr.readLine())!=null) {
Txt2.append(line+"\n");
}
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think you are better off using the JNI.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poun Kumar wrote: String cmd="C:\\TC\\BIN\\sample.exe";
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd /c \"" + cmd + "\"");
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader ebr=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line=null;
while ((line=br.readLine())!=null) {
Txt2.append(line+"\n");
}
while ((line=ebr.readLine())!=null) {
Txt2.append(line+"\n");
}

Although you try to read from both the output stream (br) and the error stream (ebr), you're not reading from them concurrently. If the process' error output buffer is full, the process may not produce output either and your code will block indefinitely. You can prevent this in one of two ways:

1) Use threads to read from both streams at the same time.

2) Use ProcessBuilder to create the process. You can call redirectErrorStream(true), which causes all error output to be merged with the regular output. You then can skip reading from the error stream completely.
 
Poun Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i give output for this c program using the above program
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poun Kumar wrote:how can i give output for this c program using the above program


Tony Docherty wrote:Read this article very carefully.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!