14 Replies - 10385 Views - Last Post: 28 October 2005 - 06:33 AM Rate Topic: -----

#1 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Calling an external program

Posted 24 October 2005 - 11:00 AM

Hi

I have written a program in C, but I would like to add an interface. To accomplish this, I have written a quick interface in java, and I would like to call this c program from within the java interface. The call will have to be made with parameters as well. Does anyone know how to do this?

Thx before
Is This A Good Question/Topic? 0
  • +

Replies To: Calling an external program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 24 October 2005 - 11:24 AM

Instead of writing it all out, I'll refer you to this thread on another forum...the general process is to use the Runtime class.

HTH!
Was This Post Helpful? 0
  • +
  • -

#3 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 24 October 2005 - 12:35 PM

Quote

  Instead of writing it all out, I'll refer you to this thread on another forum...the general process is to use the Runtime class.

HTH!


Thanks, that is exactly what I'm looking for. Only thing is, that guy is running an exe. How do you run a c-file, since c files don't have .exe extensions? I mean if I want to run a file called file.exe, I would type in the command line: file.exe, but if I run a c-file, I would type
./Test, provided the output file was named Test...

This is propably just a stupid question...

O and what does HTH mean?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 24 October 2005 - 12:50 PM

HTH means Hope This Helps :)

What platform are you using? Is this windows or *nix?

You may have to create a batch file (or shellscript/cron job) and run that instead.
Was This Post Helpful? 0
  • +
  • -

#5 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 24 October 2005 - 01:01 PM

Amadeus said:

HTH means Hope This Helps smile.gif

What platform are you using? Is this windows or *nix?

You may have to create a batch file (or shellscript/cron job) and run that instead.


I'm using Linux. I was think that I would need to create a batch file, but I have only done that in the old MS-DOS days...
Was This Post Helpful? 0
  • +
  • -

#6 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 26 October 2005 - 01:15 PM

Can anyone give me an example of what a batch file would look like in Linux?

And do you always need to put ./ in front of everything you run, or do I only need to do this because I'm working in an account-based system?
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 26 October 2005 - 04:26 PM

You'll likely want to implement it as a shell script. Here is a good look at turning a batch file into a shell script.
Was This Post Helpful? 0
  • +
  • -

#8 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 27 October 2005 - 08:40 AM

Ok, that shows how to write a script, but I still don't really know what to do. That example is if you want to TYPE a file... I want to run it... How would that look?

And how on earth do you run a script? And how would I run it from a Java environment?

I basically just want the program to execute:
./TestSuite Parameters.txt

Also, I don't really want to specify a path, I simply want the script to execute in the current directory.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 27 October 2005 - 09:05 AM

You can put the script in the same directory as the app...and you will run it by using the exec method of the Runtime class
Runtime r = Runtime.getSystemRuntime();
r.exec("insertscriptnamehere");


Was This Post Helpful? 0
  • +
  • -

#10 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 27 October 2005 - 01:27 PM

Ok, I put the following code in my java program:
Runtime r = Runtime.getRuntime();
r.exec("InvokeTest.sh");



But I get the error:
java.io.IOException: InvokeTest.sh: not found

I know the file InvokeTest.sh exists and it is in the same directory... Maybe the problem is that my script is not correct?

Right now my 'script' contains
./TestSuite Parameters.txt



This was a real shot in the dark since I really can't figure out what my script is supposed to look like.
Was This Post Helpful? 0
  • +
  • -

#11 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 27 October 2005 - 01:33 PM

Ok, apparently you need to give the entire path to the file. Now I get the message
java.io.IOException: cannot execute

So obvioustly my script is wrong... Can someone please show me how to do this? I'm really going crazy here...
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 27 October 2005 - 03:43 PM

Well, let's try a few things

1. Make sure the permissions on both the program and shell script are set to allow for execution...try chmodding them at 777.
2. Once that is done, try and exec just the program.
3. This is an article on some common problems using the exec method...it may help troubleshoot.
Was This Post Helpful? 0
  • +
  • -

#13 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 28 October 2005 - 05:27 AM

Ok, using the link you provided I have got it running now. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Calling an external program

Posted 28 October 2005 - 05:47 AM

Excellent! What did you have to do (just curious)?
Was This Post Helpful? 0
  • +
  • -

#15 Vegter  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 77
  • Joined: 21-September 05

Re: Calling an external program

Posted 28 October 2005 - 06:33 AM

Quote

Excellent! What did you have to do (just curious)?


Well, several things actually. Firstly, I had to run
chmod u+x scriptName.sh
to get the script to be able to execute.

Also, I had to rewrite the script to the form
#/bin/bash
'pwd'/Application parameter



Then I had to use the StreamGobbler setup that the person used in the link you provided. I actually first tried to simply run the C file directly, but this proved impossible... The main thing that I learned was that you actually need to stream the output from the program you call to the console. I thought you simply call it and forget about it. Anyways, now it seems to work perfectly.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1