# Calling Exec() in Windows versus UNIX

**URL:** <https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390>\
**Category:** Ruby\
**Created:** [September 24, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390 "2007-09-24T20:16:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/1 "2007-09-24T20:16:36Z")

</div>

I have the following line in a small app:

exec(“charting/RequestByProject.jar”,params[:name]’)

When run in a UNIX environment it works fine.

However, moving it over to Windows and trying to run:  
exec(“charting\RequestByProject.jar”,params[:name]’)

Returns nothing… But if I jump to the Windows command line and run:  
RequestByProject.jar project

It runs fine.

Any thoughts why it’s not executing from my Ruby code in Windows?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/2 "2007-09-24T20:23:32Z")

</div>

Matthew W. wrote:

> I have the following line in a small app:
> 
> exec(“charting/RequestByProject.jar”,params[:name]’)
> 
> When run in a UNIX environment it works fine.
> 
> However, moving it over to Windows and trying to run:  
> exec(“charting\RequestByProject.jar”,params[:name]’)
> 
> Returns nothing… But if I jump to the Windows command line and run:  
> RequestByProject.jar project
> 
> It runs fine.
> 
> Any thoughts why it’s not executing from my Ruby code in Windows?

Did you try with system() or `` instead? Or maybe IO.  
And tried File.exist? on it too, to see if the path was working?

Last but not least, maybe it wasn’t in the path. But if it can be found,  
it should even work on Windows using something like  
result = `RequestByProject.jar #{params[:name]}`  
and peeking at the result, and the error code

Just some thoughts 🙂

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/3 "2007-09-24T20:28:10Z")

</div>

> However, moving it over to Windows and trying to run:  
> exec(“charting\RequestByProject.jar”,params[:name]’)

You’re escaping the ‘R’ in the above command.

You can leave it exec(“charting/RequestByProject.jar”,params[:name]’)  
and it would still work on window.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/4 "2007-09-24T20:37:57Z")

</div>

Doan, Alex wrote:

> > However, moving it over to Windows and trying to run:  
> > exec(“charting\RequestByProject.jar”,params[:name]’)
> 
> You’re escaping the ‘R’ in the above command.
> 
> You can leave it exec(“charting/RequestByProject.jar”,params[:name]’)  
> and it would still work on window.

I initially left it like that but when it wasn’t working thought I had  
to flip the / but it still didn’t work. Then put the jar in the root of  
my app and tried running it but still nothing. But I can still go to  
the command line and run it with no problems.

If I only didn’t need to run this in Windows… 🙂

I’m also trying “java -jar charting/RequestByProject.jar” but exec()  
doesn’t seem to like the spaces, any idea how I could execute it that  
way?

Thanks

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/5 "2007-09-24T21:23:20Z")

</div>

Matthew W. wrote:

> exec(“charting/RequestByProject.jar”,params[:name]’)

This works for me:

irb(main):004:0\> exec “C:/Program Files/JGSoft/EditPadLite/EditPad.exe”

HTH.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/6 "2007-09-24T21:40:23Z")

</div>

Joel VanderWerf wrote:

> Matthew W. wrote:
> 
> > exec(“charting/RequestByProject.jar”,params[:name]’)
> 
> This works for me:
> 
> irb(main):004:0\> exec “C:/Program Files/JGSoft/EditPadLite/EditPad.exe”

This works too:

exec “C:/Program Files/JGSoft/EditPadLite/EditPad.exe”, “C:/tmp/foo.txt”

Note the difference between #exec and #system: system blocks and returns  
when the child is done, but exec causes the current process to be  
replaced with the child.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [September 24, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/calling-exec-in-windows-versus-unix/115390/7 "2007-09-24T20:48:34Z")

</div>

Marc H. wrote:

> Matthew W. wrote:
> 
> > I have the following line in a small app:
> > 
> > exec(“charting/RequestByProject.jar”,params[:name]’)
> > 
> > When run in a UNIX environment it works fine.
> > 
> > However, moving it over to Windows and trying to run:  
> > exec(“charting\RequestByProject.jar”,params[:name]’)
> > 
> > Returns nothing… But if I jump to the Windows command line and run:  
> > RequestByProject.jar project
> > 
> > It runs fine.
> > 
> > Any thoughts why it’s not executing from my Ruby code in Windows?
> 
> Did you try with system() or `` instead? Or maybe IO.  
> And tried File.exist? on it too, to see if the path was working?
> 
> Last but not least, maybe it wasn’t in the path. But if it can be found,  
> it should even work on Windows using something like  
> result = `RequestByProject.jar #{params[:name]}`  
> and peeking at the result, and the error code
> 
> Just some thoughts 🙂

@result = system(“RequestByProject.jar “+params[:name]+””)

That did the trick.

I forgot about all the other system call methods… Thanks!
