Skip to main content



      Home
Home » Newcomers » Newcomers » Programatically running command line code within eclipse.
Programatically running command line code within eclipse. [message #261860] Thu, 24 July 2008 09:28 Go to next message
Eclipse UserFriend
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 #261893 is a reply to message #261860] Thu, 24 July 2008 11:37 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gang.xu.delphi.com

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();
}

The result was the catch:

java.io.IOException: CreateProcess: C: error=5
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at cunittesting.views.MainView.createMapFile(MainView.java:431)
at cunittesting.views.MainView.access$12(MainView.java:425)
at cunittesting.views.MainView$12.run(MainView.java:299)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
at
org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:583)
at
org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:500)
at
org.eclipse.jface.action.ActionContributionItem$6.handleEven t(ActionContributionItem.java:452)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3823)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3422)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2382)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2346)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:21 98)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:493)
at
org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:288)
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:488)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
at
org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:113)
at
org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:193)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:110)
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:79)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:382)
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 549)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
Re: Programatically running command line code within eclipse. [message #261919 is a reply to message #261893] Thu, 24 July 2008 17:53 Go to previous messageGo to next message
Eclipse UserFriend
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 #261928 is a reply to message #261860] Fri, 25 July 2008 01:09 Go to previous messageGo to next message
Eclipse UserFriend
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 \"pathtonm\nm.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
Re: Programatically running command line code within eclipse. [message #262105 is a reply to message #261928] Mon, 28 July 2008 14:47 Go to previous messageGo to next message
Eclipse UserFriend
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 #262109 is a reply to message #262105] Mon, 28 July 2008 14:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gang.xu.delphi.com

Ah! I'm misspoke- something DID get printed to the command line using
Paul's method:

Exit value = 1

Like I said, however, no such mapFile.txt exists in the directory I
set-which results in my later io error or trying to read something that
isn't there.
Re: Programatically running command line code within eclipse. [message #262112 is a reply to message #262109] Mon, 28 July 2008 16:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gang.xu.delphi.com

I found a workaround-is there any way to close threads?
Re: Programatically running command line code within eclipse. [message #262114 is a reply to message #262105] Mon, 28 July 2008 16:48 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dan,

with pathtonm.exe I meant - well - probably should have written
<PathToYourNmInstallation\nm.exe> - better?

I don't like this creation of a batch and executing it thing...
Do you really need to create an output file? Would it work with
Linux/Unix that way?

OK - I removed some special classes from my ExecHelper and reworked it a
little:
http://www.jaylib.org/ExecHelper.java

The sample is quite easy to use.
Just download it and try it - for example with "C:\ dir" as parameter
or "C:\myproject\bin nm.exe binary1.exe" with the correct directory...

The first parameter is the execution directory and the other parameters
are the actual call.

Instead of writing the output to the console, you could - for instance -
parse the contents (from a class implementing the
ExecHelper.LineListener) or write them to your map-File...

Greetings
Michael
Re: Programatically running command line code within eclipse. [message #262139 is a reply to message #262105] Mon, 28 July 2008 23:02 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: Programatically running command line code within eclipse. [message #262167 is a reply to message #262139] Tue, 29 July 2008 09:03 Go to previous messageGo to next message
Eclipse UserFriend
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 #262171 is a reply to message #262114] Tue, 29 July 2008 09:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: gang.xu.delphi.com

Michael Schulte wrote:

> Hi Dan,

> with pathtonm.exe I meant - well - probably should have written
> <PathToYourNmInstallationnm.exe> - better?

> I don't like this creation of a batch and executing it thing...
> Do you really need to create an output file? Would it work with
> Linux/Unix that way?

> OK - I removed some special classes from my ExecHelper and reworked it a
> little:
> http://www.jaylib.org/ExecHelper.java

> The sample is quite easy to use.
> Just download it and try it - for example with "C: dir" as parameter
> or "C:myprojectbin nm.exe binary1.exe" with the correct directory...

> The first parameter is the execution directory and the other parameters
> are the actual call.

> Instead of writing the output to the console, you could - for instance -
> parse the contents (from a class implementing the
> ExecHelper.LineListener) or write them to your map-File...

> Greetings
> Michael

Thanks, Michael-I got my program working the batch way though :(
Why is it not good to create, run, and destroy a batch file? And what do
you mean by the Linux/Unix way? (I've only used Windows)

I took a look at your helper class and I think it does and requires a lot
more than I need (that is, your main function is much longer than my "try"
code) for this one portion of my program.

Thanks again for your time.
Re: Programatically running command line code within eclipse. [message #262200 is a reply to message #262171] Tue, 29 July 2008 13:29 Go to previous message
Eclipse UserFriend
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
Previous Topic:NoClassDefFoundError - huh?
Next Topic:Killing folded code "tooltip"
Goto Forum:
  


Current Time: Fri Sep 12 17:46:05 EDT 2025

Powered by FUDForum. Page generated in 0.07560 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top