Programatically running command line code within eclipse. [message #261860] |
Thu, 24 July 2008 09:28  |
Eclipse User |
|
|
|
Originally posted by: gang.xu.delphi.com
Hi,
I was wondering how Eclipse can programatically run command line code such
as the following. I guess my main issue is how to get Eclipse to launch
command prompt or its own version of it:
C:
cd C:\eclipseFolder\project1
nm binary1.exe > file.txt
So far, I just have the following.
private void createMapFile(String binaryPath) {
//parse the string first-into binary name and directory;
String directory = binaryPath.substring(0, binaryPath.lastIndexOf("/"));
String binary = binaryPath.substring(binaryPath.lastIndexOf("/")+1);\
//use command prompt to write into mapFile.txt:
//What would go here?
}
Thanks!
|
|
|
|
Re: Programatically running command line code within eclipse. [message #261919 is a reply to message #261893] |
Thu, 24 July 2008 17:53   |
Eclipse User |
|
|
|
Dan Xu wrote:
> Oh yeah, this is what I already tried:
>
> try {
> Runtime.getRuntime().exec("C:");
> Runtime.getRuntime().exec("cd".concat(directory));
> Runtime.getRuntime().exec("nm ".concat(binary).concat(" >
> mapFile.txt"));
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
I'm not sure of all the gory details, but the gist of this is that "C:"
is not really the name of a program. In any case, I'm pretty sure that
even if it didn't pitch an exception on the C:, it wouldn't do what you
wanted, because you'd be running those three DOS commands in three
separate processes. My suggestion is that you use write those three
lines to a batch file, use exec() to run the batch file, maybe wait on
the output and check the exit code if you're curious, then zap the batch
file. So, something like the following (omitting the try/catch block):
File f = new File("C:\\eclipseFolder\\junk.bat");
PrintWriter p = new PrintWriter(f);
p.println("C:");
p.println(cd C:\\eclipseFolder\\project1);
p.println("nm binary1.exe > file.txt");
p.close();
Process proc = Runtime.getRuntime().exec("C:\\eclipseFolder\\junk.bat");
proc.waitFor();
System.out.println("Exit value = " + proc.exitValue());
f.delete();
/Paul
|
|
|
|
Re: Programatically running command line code within eclipse. [message #262105 is a reply to message #261928] |
Mon, 28 July 2008 14:47   |
Eclipse User |
|
|
|
Originally posted by: gang.xu.delphi.com
Michael Schulte wrote:
> Hi Dan,
> I guess it's a bit more tricky than that...
> but the exec-Parameter of the Runtime takes the directory, where to run
> the program, as an extra-argument. The ">" won't work however that way.
> You might consider running sth like
> Runtime.getRuntime().exec("cmd /c "pathtonmnm.exe bin > myout.txt"",
> null, "C:mydir");
> see also (one of my project files):
>
http://cppchecker.cvs.sourceforge.net/*checkout*/cppchecker/ org.jaylib.plugins.cppchecker/src/org/jaylib/plugins/cppchec ker/util/ExecHelper.java
> Regards
> Michael
Thanks-sorry for the late reply, but a few things tripped me up. As you
may have guessed, I have not used Runtime.getRuntime.exec(...) before.
Well, I'm guessing you are using from Runtime (correct me if I'm wrong):
exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with
the specified environment and working directory.
What exactly do you mean by by pthtonm\nm.exe?
I had tried the other Paul's solution (other reply) but mapFile.txt did
not get created (I'm guessing that as you mentioned, the ">" did not
work). In fact, nothing was ever printed to the console, so I'm guessing
something got caught after being tried.
private String createMapFile(String binDirectory) {
//use command prompt to write into mapFile.txt:
//probably double back-slash to escape the escape...
File f = new File(binDirectory + "temp.bat");
PrintWriter p;
try {
p = new PrintWriter(f);
p.println("C:");
p.println("cd".concat(binDirectory));
p.println("nm binary1.exe > mapFile.txt");
p.close();
Process proc = Runtime.getRuntime().exec(binDirectory + "temp.bat");
proc.waitFor();
System.out.println("Exit value = " + proc.exitValue());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.delete();
return binDirectory.concat("/mapFile.txt");
}
Thanks again!
|
|
|
|
|
|
|
Re: Programatically running command line code within eclipse. [message #262167 is a reply to message #262139] |
Tue, 29 July 2008 09:03   |
Eclipse User |
|
|
|
Originally posted by: gang.xu.delphi.com
Paul A. Rubin wrote:
> Dan Xu wrote:
>> I had tried the other Paul's solution (other reply) but mapFile.txt did
>> not get created (I'm guessing that as you mentioned, the ">" did not
>> work). In fact, nothing was ever printed to the console, so I'm guessing
>> something got caught after being tried.
>>
>>
>>
>> private String createMapFile(String binDirectory) {
>>
>> //use command prompt to write into mapFile.txt:
>>
>> //probably double back-slash to escape the escape...
>> File f = new File(binDirectory + "temp.bat");
>>
>> PrintWriter p;
>> try {
>> p = new PrintWriter(f);
>> p.println("C:");
>> p.println("cd".concat(binDirectory));
>> p.println("nm binary1.exe > mapFile.txt");
>> p.close();
>>
>> Process proc = Runtime.getRuntime().exec(binDirectory +
>> "temp.bat");
>> proc.waitFor();
>> System.out.println("Exit value = " + proc.exitValue());
>> } catch (FileNotFoundException e) {
>> // TODO Auto-generated catch block
>> e.printStackTrace();
>> } catch (IOException e) {
>> // TODO Auto-generated catch block
>> e.printStackTrace();
>> } catch (InterruptedException e) {
>> // TODO Auto-generated catch block
>> e.printStackTrace();
>> }
>>
>> f.delete();
>>
>> return binDirectory.concat("/mapFile.txt");
>> }
>>
>>
> Your return line suggests that binDirectory is not terminated by a
> separator (since you added one in the string you concatenated). On the
> other hand, your exec command used binDirectory + "temp.bat" without
> adding a separator. If binDirectory doesn't end with "/", that could be
> a problem.
> Was temp.bat sitting in the directory? If so, is it correct? (Can you
> run it in a shell?)
> /Paul
Your commands worked perfectly in windows command prompt-the batch file
was generated, and the mapFile was created (with the correct contents).
It just seems like when I run what you have from eclipse, the lines that
redirect never get executed properly, as I don't see the batch file
anywhere, and mapFile.txt is a blank text file mysteriously placed in my
eclipse directory.
I finally DID get it working by replacing the change directory lines (C:,
cd....) and the following code works:
private String createMapFile(String binDirectory, String binName) {
//use command prompt to write into mapFile.txt:
File f = new File(binDirectory + "temp.bat");
PrintWriter p;
try {
p = new PrintWriter(f);
p.println("nm " + binDirectory + "\\" + binName + " > " + binDirectory
+ "\\mapFile.txt");
p.close();
Process proc = Runtime.getRuntime().exec(binDirectory + "temp.bat");
proc.waitFor();
System.out.println("Exit value = " + proc.exitValue());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.delete();
return binDirectory.concat("/mapFile.txt");
}
Thanks again for you help!
|
|
|
|
Re: Programatically running command line code within eclipse. [message #262200 is a reply to message #262171] |
Tue, 29 July 2008 13:29  |
Eclipse User |
|
|
|
Hi Dan,
why is it necessary to create/destroy a batch file anyway? Looks like a
workaround to me.
Well my helper class is maybe only really good, if you want to parse the
output from nm, instead of (or along with) writing it to a map file. If
you just want to create those map files, you're (quite) fine with your
solution. You only have to rewrite some lines for Linux/Unix, as there
are no batch files there, but shell-files. And what with MaxOS I don't know.
The problem of using this batch file is, you're programming on
batch-files, not in Java...
Michael
|
|
|
Powered by
FUDForum. Page generated in 0.07560 seconds