The Artima Developer Community
Sponsored Link

Java Answers Forum
using exec() in java.. PLEASE HELP ME!!!

4 replies on 1 page. Most recent reply: Mar 30, 2004 7:31 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
silly ivy

Posts: 7
Nickname: akane
Registered: Mar, 2004

using exec() in java.. PLEASE HELP ME!!! Posted: Mar 29, 2004 5:57 AM
Reply to this message Reply
Advertisement
I need help. I am using Runtime.getRUntime().exec() and I have to do two things --->

i have to (1) call cd c:\tc\ and
(2) invoke tcc noname.c

I tried to use this:

r.exec("cmd.exe /c start cd c:\tc\ && tcc noname.c");

All it does is open a DOS prompt promted at c:\tc\ but it doesnt execute the tcc noname.c... But when i do this manually by typing in the Dos prompt it works... guys help...

What is the separator between two command lines when using exec()? THanks :)


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: using exec() in java.. PLEASE HELP ME!!! Posted: Mar 30, 2004 12:38 AM
Reply to this message Reply
Hi,

Try this It will work.

Runtime runtime = Runtime.getRuntime();
//creat the command argument string object.
// this will consists of list of command you want to exec..
String[] args = new String[]{"cmd.exe", "cd c:\tc\", "tcc noname.c"};
 
//feed the args to Runtime.exec method..
Process p = runtime.exec(args);
 


I hoe this will help you..
Viswa
--------

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using exec() in java.. PLEASE HELP ME!!! Posted: Mar 30, 2004 7:03 PM
Reply to this message Reply
That's because && is working fine, but it is splitting to these two commands:
1. "cmd.exe /c start cd c:\tc\"
2. "tcc noname.c"

Instead of what you were hoping for which was to run a command shell that does both things.

The simplest solution is probably to write a little batch file to the temp directory and execute it. Also, I would use the COMSPEC environment variable instead of 'cmd.exe', so I'd create a 'compileNoname.cmd' containing this:
pushd c:\tc
tcc noname.c
and then execute it with the command interpreter (by the way, I think start is superfluous in this case -- it is more useful if you want to control how the process operates).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using exec() in java.. PLEASE HELP ME!!! Posted: Mar 30, 2004 7:04 PM
Reply to this message Reply
Oops, I accidentally posted when I wanted to go back and edit again! Oh well.

I was going to add that the reason the second part of the split command didn't run was because the command shell was gone by that time.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using exec() in java.. PLEASE HELP ME!!! Posted: Mar 30, 2004 7:31 PM
Reply to this message Reply
By the way, if you like a more complicated solution, you could write a little JNI wrapper for chdir().

JFileChooser has a setCurrentDirectory() method, too, but I haven't tried it. If that works, you could use it to change the current directory, then exec just the compile part of the command (and probably change back to your original directory).

Finally, I bet there is a way to work the command line of tcc (is that Turbo C?) such that you don't have to run it from the directory where the C file is. After all, editors, IDEs and makefiles have been doing that kind of thing for aeons.

Flat View: This topic has 4 replies on 1 page
Topic: What's better solution in reusing JSP? Previous Topic   Next Topic Topic: interview questions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use