Home » Eclipse Projects » Eclipse Platform » Programatically run a java application
| Programatically run a java application [message #311402] |
Thu, 11 January 2007 13:27  |
Eclipse User |
|
|
|
Originally posted by: s0347354.sms.ed.ac.uk
Hey all,
I am trying to programatically launch a java application which has a
simple main method which prints "hello world". I think the code below is
executing the my application, however, I need to get hold of the ouput
(I need to process the output and send it elsewhere...). What can I add
to the code below in order to listen for output?
Thanks for any help,
Will
ILaunchManager mgr=DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type =
mgr.getLaunchConfigurationType(IJavaLaunchConfigurationConst ants.ID_JAVA_APPLICATION);
ILaunchConfigurationWorkingCopy copy= type.newInstance(null,
"WebIDEConfig");
copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PRO JECT_NAME,
"SampleProject");
copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAI N_TYPE_NAME,
"MyFirstPackage/Foo");
ILaunchConfiguration config = copy.doSave();
ILaunch launch= config.launch(ILaunchManager.RUN_MODE, null);
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
IVMRunner vmRunner = vmInstall.getVMRunner(ILaunchManager.RUN_MODE);
if (vmRunner != null) {
// currentProj is an instance of IJavaProject
String[] classPath =
JavaRuntime.computeDefaultRuntimeClassPath(currentProj);
VMRunnerConfiguration vmConfig= new
VMRunnerConfiguration("MyFirstPackage/Foo", classPath);
// If we want to launch VM with arguments
//vmConfig.setVMArguments(vmArgs);
// If we want to launch the program with arguments
//vmConfig.setProgramArguments(programArgs);
vmRunner.run(vmConfig,launch, null);
}
|
|
| |
| Re: Programatically run a java application [message #311478 is a reply to message #311414] |
Sat, 13 January 2007 17:28   |
Eclipse User |
|
|
|
Originally posted by: s0347354.sms.ed.ac.uk
Hi Felix,
Thanks for your reply... but after much effort, I still can't get it to
work. What I currently have is here:
http://www.papernapkin.org/pastebin/view/4067
I am quite sure that the program is being executed since slight changes
to the attribute that specifies the class with the main method (on line
47) causes the JVM to bring up an alert box saying "Could not find the
main class". The main problem I think is the console listener, as no
consoles ever seem to be added, hence I cannot capture any output...
Which action is it that would create a console? Is it when you call
IVMRunner.run()? Could I also see the original code that you used, to
check that I am not missing anything?
Thanks for any additional help!
Will
Felix Leipold wrote:
> Hi Will,
>
>
>> I am trying to programatically launch a java application which has a
>> simple main method which prints "hello world". I think the code below
>> is executing the my application, however, I need to get hold of the
>> ouput (I need to process the output and send it elsewhere...). What
>> can I add to the code below in order to listen for output?
>
> I had a similar problem, when writing an automated test:
> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>
>
> The output is printed to a console. You can get a reference to the
> console by using the ConsoleManager. I used a bit of a hack to get a
> reference to the console. Then I do more hacky stuff to wait until the
> console gets updated with the actual output my code looks somewhat like
> this:
>
> IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
> final TextConsole[] console=new TextConsole[1];
> manager.addConsoleListener(new IConsoleListener(){
> public void consolesAdded(IConsole[] consoles) {
> console[0]=((TextConsole)consoles[0]);
> console[0].activate();
> }
> public void consolesRemoved(IConsole[] consoles) {}
> });
>
> vmRunner.run(vmConfig,launch, null);
> waitForJobs();
>
> delay(1000);
> String output = console[0].getDocument().get();
>
> Where waitForJobs() and delay uses Display.sleep() and
> Display.readAndDispatch() like this:
>
> private void delay(long durationInMilliseconds){
> Display display= Display.getCurrent();
> if (display!=null){
> long t2=System.currentTimeMillis()+durationInMilliseconds;
> while (System.currentTimeMillis()<t2){
> if (!display.readAndDispatch()){
> display.sleep();
> }
> }
> display.update();
> } else {
> try {
> Thread.sleep(durationInMilliseconds);
> } catch (InterruptedException e) {}
> }
> }
>
> public void waitForJobs(){
> while(Platform.getJobManager().currentJob() != null)
> delay(1000);
> }
>
> But perhaps it is easier just to spawn the java class manually...
>
> Cheers,
>
> Felix
>
|
|
|
| Re: Programatically run a java application [message #311480 is a reply to message #311478] |
Sat, 13 January 2007 18:00   |
Eclipse User |
|
|
|
Originally posted by: felix.leipold.gmx.de
Hi Will,
> Thanks for your reply... but after much effort, I still can't get it to
> work. What I currently have is here:
> http://www.papernapkin.org/pastebin/view/4067
> I am quite sure that the program is being executed since slight changes
> to the attribute that specifies the class with the main method (on line
> 47) causes the JVM to bring up an alert box saying "Could not find the
> main class". The main problem I think is the console listener, as no
> consoles ever seem to be added, hence I cannot capture any output...
> Which action is it that would create a console? Is it when you call
> IVMRunner.run()? Could I also see the original code that you used, to
> check that I am not missing anything?
I had a similar problem and it turned out, that there was an Exception
deep down in the framework. As a debugging strategy I recommend setting an
Exception Breakpoint and see if there is any problem.
More info on how to launch java apps can be found here:
http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
My own stuff is part of the jbehave eclipse plug-in, which can be found at
jbehave.org.
Hope that helps,
Felix
> Felix Leipold wrote:
>> Hi Will,
>>
>>
>>> I am trying to programatically launch a java application which has a
>>> simple main method which prints "hello world". I think the code below
>>> is executing the my application, however, I need to get hold of the
>>> ouput (I need to process the output and send it elsewhere...). What
>>> can I add to the code below in order to listen for output?
>>
>> I had a similar problem, when writing an automated test:
>>
http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>>
>>
>> The output is printed to a console. You can get a reference to the
>> console by using the ConsoleManager. I used a bit of a hack to get a
>> reference to the console. Then I do more hacky stuff to wait until the
>> console gets updated with the actual output my code looks somewhat like
>> this:
>>
>> IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
>> final TextConsole[] console=new TextConsole[1];
>> manager.addConsoleListener(new IConsoleListener(){
>> public void consolesAdded(IConsole[] consoles) {
>> console[0]=((TextConsole)consoles[0]);
>> console[0].activate();
>> }
>> public void consolesRemoved(IConsole[] consoles) {}
>> });
>>
>> vmRunner.run(vmConfig,launch, null);
>> waitForJobs();
>>
>> delay(1000);
>> String output = console[0].getDocument().get();
>>
>> Where waitForJobs() and delay uses Display.sleep() and
>> Display.readAndDispatch() like this:
>>
>> private void delay(long durationInMilliseconds){
>> Display display= Display.getCurrent();
>> if (display!=null){
>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>> while (System.currentTimeMillis()<t2){
>> if (!display.readAndDispatch()){
>> display.sleep();
>> }
>> }
>> display.update();
>> } else {
>> try {
>> Thread.sleep(durationInMilliseconds);
>> } catch (InterruptedException e) {}
>> }
>> }
>>
>> public void waitForJobs(){
>> while(Platform.getJobManager().currentJob() != null)
>> delay(1000);
>> }
>>
>> But perhaps it is easier just to spawn the java class manually...
>>
>> Cheers,
>>
>> Felix
>>
|
|
|
| Re: Programatically run a java application [message #311482 is a reply to message #311480] |
Sat, 13 January 2007 20:56   |
Eclipse User |
|
|
|
Originally posted by: s0347354.sms.ed.ac.uk
Thanks Felix, but after trying to debug, I cannot find what is causing
the problem. Can anyone else spot anything that could be going wrong, or
perhaps suggest a different approach to the problem.
Felix Leipold wrote:
> Hi Will,
>
>> Thanks for your reply... but after much effort, I still can't get it
>> to work. What I currently have is here:
>
>> http://www.papernapkin.org/pastebin/view/4067
>
>> I am quite sure that the program is being executed since slight
>> changes to the attribute that specifies the class with the main method
>> (on line 47) causes the JVM to bring up an alert box saying "Could not
>> find the main class". The main problem I think is the console
>> listener, as no consoles ever seem to be added, hence I cannot capture
>> any output...
>
>> Which action is it that would create a console? Is it when you call
>> IVMRunner.run()? Could I also see the original code that you used, to
>> check that I am not missing anything?
>
>
> I had a similar problem and it turned out, that there was an Exception
> deep down in the framework. As a debugging strategy I recommend setting
> an Exception Breakpoint and see if there is any problem.
>
> More info on how to launch java apps can be found here:
> http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
>
> My own stuff is part of the jbehave eclipse plug-in, which can be found
> at jbehave.org.
>
> Hope that helps,
>
> Felix
>> Felix Leipold wrote:
>>> Hi Will,
>>>
>>>
>>>> I am trying to programatically launch a java application which has a
>>>> simple main method which prints "hello world". I think the code
>>>> below is executing the my application, however, I need to get hold
>>>> of the ouput (I need to process the output and send it
>>>> elsewhere...). What can I add to the code below in order to listen
>>>> for output?
>>>
>>> I had a similar problem, when writing an automated test:
>>>
> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>
>>>
>>>
>>> The output is printed to a console. You can get a reference to the
>>> console by using the ConsoleManager. I used a bit of a hack to get a
>>> reference to the console. Then I do more hacky stuff to wait until
>>> the console gets updated with the actual output my code looks
>>> somewhat like this:
>>>
>>> IConsoleManager manager =
>>> ConsolePlugin.getDefault().getConsoleManager();
>>> final TextConsole[] console=new TextConsole[1];
>>> manager.addConsoleListener(new IConsoleListener(){
>>> public void consolesAdded(IConsole[] consoles) {
>>> console[0]=((TextConsole)consoles[0]);
>>> console[0].activate();
>>> }
>>> public void consolesRemoved(IConsole[] consoles) {}
>>> });
>>>
>>> vmRunner.run(vmConfig,launch, null);
>>> waitForJobs();
>>>
>>> delay(1000);
>>> String output = console[0].getDocument().get();
>>>
>>> Where waitForJobs() and delay uses Display.sleep() and
>>> Display.readAndDispatch() like this:
>>>
>>> private void delay(long durationInMilliseconds){
>>> Display display= Display.getCurrent();
>>> if (display!=null){
>>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>>> while (System.currentTimeMillis()<t2){
>>> if (!display.readAndDispatch()){
>>> display.sleep();
>>> }
>>> }
>>> display.update();
>>> } else {
>>> try {
>>> Thread.sleep(durationInMilliseconds);
>>> } catch (InterruptedException e) {}
>>> }
>>> }
>>> public void waitForJobs(){
>>> while(Platform.getJobManager().currentJob() != null)
>>> delay(1000);
>>> }
>>>
>>> But perhaps it is easier just to spawn the java class manually...
>>>
>>> Cheers,
>>>
>>> Felix
>>>
>
>
|
|
|
| Re: Programatically run a java application [message #311484 is a reply to message #311480] |
Sat, 13 January 2007 21:00   |
Eclipse User |
|
|
|
Hi,
Have you considered using/trying java.lang.Runtime.exec(...) ?
Charlie
Felix Leipold wrote:
> Hi Will,
>
>> Thanks for your reply... but after much effort, I still can't get it
>> to work. What I currently have is here:
>
>
>> http://www.papernapkin.org/pastebin/view/4067
>
>
>> I am quite sure that the program is being executed since slight
>> changes to the attribute that specifies the class with the main method
>> (on line 47) causes the JVM to bring up an alert box saying "Could not
>> find the main class". The main problem I think is the console
>> listener, as no consoles ever seem to be added, hence I cannot capture
>> any output...
>
>
>> Which action is it that would create a console? Is it when you call
>> IVMRunner.run()? Could I also see the original code that you used, to
>> check that I am not missing anything?
>
>
>
> I had a similar problem and it turned out, that there was an Exception
> deep down in the framework. As a debugging strategy I recommend setting
> an Exception Breakpoint and see if there is any problem.
>
> More info on how to launch java apps can be found here:
> http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
>
> My own stuff is part of the jbehave eclipse plug-in, which can be found
> at jbehave.org.
>
> Hope that helps,
>
> Felix
>
>> Felix Leipold wrote:
>>
>>> Hi Will,
>>>
>>>
>>>> I am trying to programatically launch a java application which has a
>>>> simple main method which prints "hello world". I think the code
>>>> below is executing the my application, however, I need to get hold
>>>> of the ouput (I need to process the output and send it
>>>> elsewhere...). What can I add to the code below in order to listen
>>>> for output?
>>>
>>>
>>> I had a similar problem, when writing an automated test:
>>>
> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>
>
>>>
>>>
>>> The output is printed to a console. You can get a reference to the
>>> console by using the ConsoleManager. I used a bit of a hack to get a
>>> reference to the console. Then I do more hacky stuff to wait until
>>> the console gets updated with the actual output my code looks
>>> somewhat like this:
>>>
>>> IConsoleManager manager =
>>> ConsolePlugin.getDefault().getConsoleManager();
>>> final TextConsole[] console=new TextConsole[1];
>>> manager.addConsoleListener(new IConsoleListener(){
>>> public void consolesAdded(IConsole[] consoles) {
>>> console[0]=((TextConsole)consoles[0]);
>>> console[0].activate();
>>> }
>>> public void consolesRemoved(IConsole[] consoles) {}
>>> });
>>>
>>> vmRunner.run(vmConfig,launch, null);
>>> waitForJobs();
>>>
>>> delay(1000);
>>> String output = console[0].getDocument().get();
>>>
>>> Where waitForJobs() and delay uses Display.sleep() and
>>> Display.readAndDispatch() like this:
>>>
>>> private void delay(long durationInMilliseconds){
>>> Display display= Display.getCurrent();
>>> if (display!=null){
>>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>>> while (System.currentTimeMillis()<t2){
>>> if (!display.readAndDispatch()){
>>> display.sleep();
>>> }
>>> }
>>> display.update();
>>> } else {
>>> try {
>>> Thread.sleep(durationInMilliseconds);
>>> } catch (InterruptedException e) {}
>>> }
>>> }
>>> public void waitForJobs(){
>>> while(Platform.getJobManager().currentJob() != null)
>>> delay(1000);
>>> }
>>>
>>> But perhaps it is easier just to spawn the java class manually...
>>>
>>> Cheers,
>>>
>>> Felix
>>>
>
>
|
|
|
| Re: Programatically run a java application [message #311742 is a reply to message #311484] |
Sat, 20 January 2007 12:51   |
Eclipse User |
|
|
|
Originally posted by: s0347354.sms.ed.ac.uk
Thanks Charlie... that did the trick. It was also more useful than my
other approach as I could easily capture the output (see bottom).
Anywayz, I was wondering if it were possible to use the exec command to
create a jar file...
I have tried a number of approaches but they don't seem to work. The
first approach I have been trying is:
Process p = Runtime.getRuntime().exec("jar cf Files.jar temp.java",
null, new File("C:\\Program Files\\eclipse\\workspace"));
However, it throws the exception:
java.io.IOException: CreateProcess: jar cf Files.jar temp.java error=2
Which I think is a file not found exception (error 2). I'm not sure if
I'm even able to use the exec command to create a jar file... maybe this
is what the file not found error is- it cannot find the jar creation
utility.
C:\\Program Files\\eclipse\\workspace refers to a directory, and
temp.java refers to a file that exists in that directory.
Any ideas are much appreciated. Thanks,
Will
Process p = currentRuntime.exec("java MyFirstPackage.Foo", null, new
File("C:\\Program Files\\eclipse\\workspace\\"));
p.getInputStream();
p.getErrorStream();
p.getOutputStream();
etc
Charle Kelly wrote:
> Hi,
>
> Have you considered using/trying java.lang.Runtime.exec(...) ?
>
> Charlie
>
>
> Felix Leipold wrote:
>> Hi Will,
>>
>>> Thanks for your reply... but after much effort, I still can't get it
>>> to work. What I currently have is here:
>>
>>
>>> http://www.papernapkin.org/pastebin/view/4067
>>
>>
>>> I am quite sure that the program is being executed since slight
>>> changes to the attribute that specifies the class with the main
>>> method (on line 47) causes the JVM to bring up an alert box saying
>>> "Could not find the main class". The main problem I think is the
>>> console listener, as no consoles ever seem to be added, hence I
>>> cannot capture any output...
>>
>>
>>> Which action is it that would create a console? Is it when you call
>>> IVMRunner.run()? Could I also see the original code that you used, to
>>> check that I am not missing anything?
>>
>>
>>
>> I had a similar problem and it turned out, that there was an Exception
>> deep down in the framework. As a debugging strategy I recommend
>> setting an Exception Breakpoint and see if there is any problem.
>>
>> More info on how to launch java apps can be found here:
>> http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
>>
>> My own stuff is part of the jbehave eclipse plug-in, which can be
>> found at jbehave.org.
>>
>> Hope that helps,
>>
>> Felix
>>
>>> Felix Leipold wrote:
>>>
>>>> Hi Will,
>>>>
>>>>
>>>>> I am trying to programatically launch a java application which has
>>>>> a simple main method which prints "hello world". I think the code
>>>>> below is executing the my application, however, I need to get hold
>>>>> of the ouput (I need to process the output and send it
>>>>> elsewhere...). What can I add to the code below in order to listen
>>>>> for output?
>>>>
>>>>
>>>> I had a similar problem, when writing an automated test:
>>>>
>> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>>
>>
>>>>
>>>>
>>>> The output is printed to a console. You can get a reference to the
>>>> console by using the ConsoleManager. I used a bit of a hack to get a
>>>> reference to the console. Then I do more hacky stuff to wait until
>>>> the console gets updated with the actual output my code looks
>>>> somewhat like this:
>>>>
>>>> IConsoleManager manager =
>>>> ConsolePlugin.getDefault().getConsoleManager();
>>>> final TextConsole[] console=new TextConsole[1];
>>>> manager.addConsoleListener(new IConsoleListener(){
>>>> public void consolesAdded(IConsole[] consoles) {
>>>> console[0]=((TextConsole)consoles[0]);
>>>> console[0].activate();
>>>> }
>>>> public void consolesRemoved(IConsole[] consoles) {}
>>>> });
>>>>
>>>> vmRunner.run(vmConfig,launch, null);
>>>> waitForJobs();
>>>>
>>>> delay(1000);
>>>> String output = console[0].getDocument().get();
>>>>
>>>> Where waitForJobs() and delay uses Display.sleep() and
>>>> Display.readAndDispatch() like this:
>>>>
>>>> private void delay(long durationInMilliseconds){
>>>> Display display= Display.getCurrent();
>>>> if (display!=null){
>>>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>>>> while (System.currentTimeMillis()<t2){
>>>> if (!display.readAndDispatch()){
>>>> display.sleep();
>>>> }
>>>> }
>>>> display.update();
>>>> } else {
>>>> try {
>>>> Thread.sleep(durationInMilliseconds);
>>>> } catch (InterruptedException e) {}
>>>> }
>>>> }
>>>> public void waitForJobs(){
>>>> while(Platform.getJobManager().currentJob() != null)
>>>> delay(1000);
>>>> }
>>>>
>>>> But perhaps it is easier just to spawn the java class manually...
>>>>
>>>> Cheers,
>>>>
>>>> Felix
>>>>
>>
>>
|
|
|
| Re: Programatically run a java application [message #311743 is a reply to message #311742] |
Sat, 20 January 2007 13:21   |
Eclipse User |
|
|
|
Hi Will,
I'm glad previous suggestion worked.
You might try
java.util.jar.JarFile
Charlie
Will Ryan wrote:
> Thanks Charlie... that did the trick. It was also more useful than my
> other approach as I could easily capture the output (see bottom).
> Anywayz, I was wondering if it were possible to use the exec command to
> create a jar file...
>
>
> I have tried a number of approaches but they don't seem to work. The
> first approach I have been trying is:
>
> Process p = Runtime.getRuntime().exec("jar cf Files.jar temp.java",
> null, new File("C:\\Program Files\\eclipse\\workspace"));
>
> However, it throws the exception:
> java.io.IOException: CreateProcess: jar cf Files.jar temp.java error=2
>
> Which I think is a file not found exception (error 2). I'm not sure if
> I'm even able to use the exec command to create a jar file... maybe this
> is what the file not found error is- it cannot find the jar creation
> utility.
>
> C:\\Program Files\\eclipse\\workspace refers to a directory, and
> temp.java refers to a file that exists in that directory.
>
> Any ideas are much appreciated. Thanks,
>
> Will
>
>
>
>
>
>
> Process p = currentRuntime.exec("java MyFirstPackage.Foo", null, new
> File("C:\\Program Files\\eclipse\\workspace\\"));
>
> p.getInputStream();
> p.getErrorStream();
> p.getOutputStream();
>
> etc
>
>
> Charle Kelly wrote:
>
>> Hi,
>>
>> Have you considered using/trying java.lang.Runtime.exec(...) ?
>>
>> Charlie
>>
>>
>> Felix Leipold wrote:
>>
>>> Hi Will,
>>>
>>>> Thanks for your reply... but after much effort, I still can't get it
>>>> to work. What I currently have is here:
>>>
>>>
>>>
>>>> http://www.papernapkin.org/pastebin/view/4067
>>>
>>>
>>>
>>>> I am quite sure that the program is being executed since slight
>>>> changes to the attribute that specifies the class with the main
>>>> method (on line 47) causes the JVM to bring up an alert box saying
>>>> "Could not find the main class". The main problem I think is the
>>>> console listener, as no consoles ever seem to be added, hence I
>>>> cannot capture any output...
>>>
>>>
>>>
>>>> Which action is it that would create a console? Is it when you call
>>>> IVMRunner.run()? Could I also see the original code that you used,
>>>> to check that I am not missing anything?
>>>
>>>
>>>
>>>
>>> I had a similar problem and it turned out, that there was an
>>> Exception deep down in the framework. As a debugging strategy I
>>> recommend setting an Exception Breakpoint and see if there is any
>>> problem.
>>>
>>> More info on how to launch java apps can be found here:
>>> http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
>>>
>>> My own stuff is part of the jbehave eclipse plug-in, which can be
>>> found at jbehave.org.
>>>
>>> Hope that helps,
>>>
>>> Felix
>>>
>>>> Felix Leipold wrote:
>>>>
>>>>> Hi Will,
>>>>>
>>>>>
>>>>>> I am trying to programatically launch a java application which has
>>>>>> a simple main method which prints "hello world". I think the code
>>>>>> below is executing the my application, however, I need to get hold
>>>>>> of the ouput (I need to process the output and send it
>>>>>> elsewhere...). What can I add to the code below in order to listen
>>>>>> for output?
>>>>>
>>>>>
>>>>>
>>>>> I had a similar problem, when writing an automated test:
>>>>>
>>> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>>>
>>>
>>>>>
>>>>>
>>>>> The output is printed to a console. You can get a reference to the
>>>>> console by using the ConsoleManager. I used a bit of a hack to get
>>>>> a reference to the console. Then I do more hacky stuff to wait
>>>>> until the console gets updated with the actual output my code looks
>>>>> somewhat like this:
>>>>>
>>>>> IConsoleManager manager =
>>>>> ConsolePlugin.getDefault().getConsoleManager();
>>>>> final TextConsole[] console=new TextConsole[1];
>>>>> manager.addConsoleListener(new IConsoleListener(){
>>>>> public void consolesAdded(IConsole[] consoles) {
>>>>> console[0]=((TextConsole)consoles[0]);
>>>>> console[0].activate();
>>>>> }
>>>>> public void consolesRemoved(IConsole[] consoles) {}
>>>>> });
>>>>>
>>>>> vmRunner.run(vmConfig,launch, null);
>>>>> waitForJobs();
>>>>>
>>>>> delay(1000);
>>>>> String output = console[0].getDocument().get();
>>>>>
>>>>> Where waitForJobs() and delay uses Display.sleep() and
>>>>> Display.readAndDispatch() like this:
>>>>>
>>>>> private void delay(long durationInMilliseconds){
>>>>> Display display= Display.getCurrent();
>>>>> if (display!=null){
>>>>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>>>>> while (System.currentTimeMillis()<t2){
>>>>> if (!display.readAndDispatch()){
>>>>> display.sleep();
>>>>> }
>>>>> }
>>>>> display.update();
>>>>> } else {
>>>>> try {
>>>>> Thread.sleep(durationInMilliseconds);
>>>>> } catch (InterruptedException e) {}
>>>>> }
>>>>> }
>>>>> public void waitForJobs(){
>>>>> while(Platform.getJobManager().currentJob() != null)
>>>>> delay(1000);
>>>>> }
>>>>>
>>>>> But perhaps it is easier just to spawn the java class manually...
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Felix
>>>>>
>>>
>>>
|
|
|
| Re: Programatically run a java application [message #323100 is a reply to message #311742] |
Sun, 09 December 2007 08:02  |
Eclipse User |
|
|
|
Originally posted by: daniel.winterstein.gmail.com
Hello Will,
You've probably already fixed this. If not, I think the problem is in
your exec call. Process is a fussy bastard of a class. Try splitting
the command up. Instead of
"jar cf Files.jar temp.java"
Use
new String[]{"jar", "cf", "Files.jar", "temp.java"}
Oh, and be aware that Process has several gotchas. Java does not play
nicely with the host OS. Your code probably won't run on Windows without
some tweaking. And you should get the Process's ouput and error streams
and set up readers to empty them, or the JVM can hang. I've been meaning
to write a post about this for http://javablog.co.uk
Anyway: All the best, etc.
- Dan
Will Ryan wrote:
> Thanks Charlie... that did the trick. It was also more useful than my
> other approach as I could easily capture the output (see bottom).
> Anywayz, I was wondering if it were possible to use the exec command to
> create a jar file...
>
>
> I have tried a number of approaches but they don't seem to work. The
> first approach I have been trying is:
>
> Process p = Runtime.getRuntime().exec("jar cf Files.jar temp.java",
> null, new File("C:\\Program Files\\eclipse\\workspace"));
>
> However, it throws the exception:
> java.io.IOException: CreateProcess: jar cf Files.jar temp.java error=2
>
> Which I think is a file not found exception (error 2). I'm not sure if
> I'm even able to use the exec command to create a jar file... maybe this
> is what the file not found error is- it cannot find the jar creation
> utility.
>
> C:\\Program Files\\eclipse\\workspace refers to a directory, and
> temp.java refers to a file that exists in that directory.
>
> Any ideas are much appreciated. Thanks,
>
> Will
>
>
>
>
>
>
> Process p = currentRuntime.exec("java MyFirstPackage.Foo", null, new
> File("C:\\Program Files\\eclipse\\workspace\\"));
>
> p.getInputStream();
> p.getErrorStream();
> p.getOutputStream();
>
> etc
>
>
> Charle Kelly wrote:
>> Hi,
>>
>> Have you considered using/trying java.lang.Runtime.exec(...) ?
>>
>> Charlie
>>
>>
>> Felix Leipold wrote:
>>> Hi Will,
>>>
>>>> Thanks for your reply... but after much effort, I still can't get it
>>>> to work. What I currently have is here:
>>>
>>>
>>>> http://www.papernapkin.org/pastebin/view/4067
>>>
>>>
>>>> I am quite sure that the program is being executed since slight
>>>> changes to the attribute that specifies the class with the main
>>>> method (on line 47) causes the JVM to bring up an alert box saying
>>>> "Could not find the main class". The main problem I think is the
>>>> console listener, as no consoles ever seem to be added, hence I
>>>> cannot capture any output...
>>>
>>>
>>>> Which action is it that would create a console? Is it when you call
>>>> IVMRunner.run()? Could I also see the original code that you used,
>>>> to check that I am not missing anything?
>>>
>>>
>>>
>>> I had a similar problem and it turned out, that there was an
>>> Exception deep down in the framework. As a debugging strategy I
>>> recommend setting an Exception Breakpoint and see if there is any
>>> problem.
>>>
>>> More info on how to launch java apps can be found here:
>>> http://www.eclipse.org/articles/Article-Java-launch/launchin g-java.html
>>>
>>> My own stuff is part of the jbehave eclipse plug-in, which can be
>>> found at jbehave.org.
>>>
>>> Hope that helps,
>>>
>>> Felix
>>>
>>>> Felix Leipold wrote:
>>>>
>>>>> Hi Will,
>>>>>
>>>>>
>>>>>> I am trying to programatically launch a java application which has
>>>>>> a simple main method which prints "hello world". I think the code
>>>>>> below is executing the my application, however, I need to get hold
>>>>>> of the ouput (I need to process the output and send it
>>>>>> elsewhere...). What can I add to the code below in order to listen
>>>>>> for output?
>>>>>
>>>>>
>>>>> I had a similar problem, when writing an automated test:
>>>>>
>>> http://www.eclipse.org/newsportal/article.php?id=61232&g roup=eclipse.platform#61232
>>>
>>>
>>>>>
>>>>>
>>>>> The output is printed to a console. You can get a reference to the
>>>>> console by using the ConsoleManager. I used a bit of a hack to get
>>>>> a reference to the console. Then I do more hacky stuff to wait
>>>>> until the console gets updated with the actual output my code looks
>>>>> somewhat like this:
>>>>>
>>>>> IConsoleManager manager =
>>>>> ConsolePlugin.getDefault().getConsoleManager();
>>>>> final TextConsole[] console=new TextConsole[1];
>>>>> manager.addConsoleListener(new IConsoleListener(){
>>>>> public void consolesAdded(IConsole[] consoles) {
>>>>> console[0]=((TextConsole)consoles[0]);
>>>>> console[0].activate();
>>>>> }
>>>>> public void consolesRemoved(IConsole[] consoles) {}
>>>>> });
>>>>>
>>>>> vmRunner.run(vmConfig,launch, null);
>>>>> waitForJobs();
>>>>>
>>>>> delay(1000);
>>>>> String output = console[0].getDocument().get();
>>>>>
>>>>> Where waitForJobs() and delay uses Display.sleep() and
>>>>> Display.readAndDispatch() like this:
>>>>>
>>>>> private void delay(long durationInMilliseconds){
>>>>> Display display= Display.getCurrent();
>>>>> if (display!=null){
>>>>> long t2=System.currentTimeMillis()+durationInMilliseconds;
>>>>> while (System.currentTimeMillis()<t2){
>>>>> if (!display.readAndDispatch()){
>>>>> display.sleep();
>>>>> }
>>>>> }
>>>>> display.update();
>>>>> } else {
>>>>> try {
>>>>> Thread.sleep(durationInMilliseconds);
>>>>> } catch (InterruptedException e) {}
>>>>> }
>>>>> }
>>>>> public void waitForJobs(){
>>>>> while(Platform.getJobManager().currentJob() != null)
>>>>> delay(1000);
>>>>> }
>>>>>
>>>>> But perhaps it is easier just to spawn the java class manually...
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Felix
>>>>>
>>>
>>>
|
|
|
Goto Forum:
Current Time: Tue Oct 28 18:03:00 EDT 2025
Powered by FUDForum. Page generated in 0.06533 seconds
|