Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a C++ executable from JSP?

 
Priya Raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to call a C++ executable from a JSP page. I am testing a small Java Code to achieve this. I am trying to do this by using Runtime.exec(). The C++ code creates ASCII files (using ofstream) as log files during after completion.
The c++ code works fine as a standalone when I execute the executable from command line. It creates the file properly.
Only when I call it from Java code using Runtime.exec(), I see the problem.
When I do a read on process.getInputStream(), I am able to see the output that is written to the "cout" in my C++ code. But the output file is not created as it was when the C++ code was executed alone.
Could anybody let me know if what I am doing is right? if so, why is the file not being created when run from Java?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your C++ program trying to write to a file relative to the "current directory" or to a hard-coded location in the file system? I ask because one of the most common problems with writing files from code running in a server is that the server may not have its "current directory" set to anything useful.
Can you try writing to a fixed location (say /tmp/whatever on unix or c:\tmp\whatever on Windows) and let us know if you can see any output then ?
 
Priya Raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is on a UNIX machine and the C++ code outputs to a relative directory. The same C++ code works fine as I said and creates the file in the correct directory, when executing the C++ executable alone. Its only when I run the Java Code to execute this C++ executable, do I get this problem. This sample code is not yet on a webserver. Both C++ and Java Code are on the same Unix Machine.
 
Matthew Webster
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ are you writing to a file on disc AND writing to the standard out, or just one of those?
If you are not writing to the standard out you will not receive anything on the Java's standard in.
You may need to flush the output of your C++ output.
If you are writing to a file in C++, then reading the file in Java, is the Java waiting until the file is finished being written before it starts reading? If not it will be trying to read a file which has no content yet.
 
Priya Raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mathew,
I am writing some stuff to a file on disk and some stuff to the std. out in my C++ code.
When executing the C++ code as a standalone C++ executable I am seeing both outputs.
But when executing the code from Java,
I am able to read the stuff in std. out from the Java Code. I use the follwoing
Process p = Runtime.getRunime().exec(<filename> ;
p.waitFor();
I am not able to see the file that was supposed to be created by the C++ code on the disk. I am not able to see the physical file at all, let alone reading from it in Java code. The C++ code has done a flush and a close on the ofstream. Do I have to take care of anything else here? Am I missing something?
Thanks in advance
RHP
 
Priya Raj
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mathew for the detailed explanation, it clarified a lot of things about Runtime.exec().
My issue is solved. Surprisingly, the code when executed from Java required, absolute path for execution, guess it depends on where the subprocess is spawned from. Now with the absolute paths in, it works fine.
Thanks
RHP
 
Always! Wait. Never. Shut up. Look at this tiny ad.
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!