The Artima Developer Community
Sponsored Link

Java Answers Forum
Exec. command in form of string

2 replies on 1 page. Most recent reply: May 25, 2004 2:27 AM by Matthias Neumair

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 2 replies on 1 page
Sam Crickow

Posts: 4
Nickname: limpit23
Registered: Apr, 2004

Exec. command in form of string Posted: May 24, 2004 12:17 PM
Reply to this message Reply
Advertisement
Is there any way to execute a command in java that is in the form of a string? e.g. to execute "System.out.println(5);".

I tried the following but none was satisfactory :

1/ Runtime.getRuntime().exec("System.out.println(5);");

It seems that 'exec' can only be used with files containing commands (& not the commands themselves) e.g. .exe or .bin, etc. The compiler gave as output 'System.out.println(5) not found.'. An IOException is thrown.

2/ Compiler.command("System.out.println(5);");

No output at all.

3/ Methods like 'runWithCommand' or 'Execute' do not seem to work.

One may wonder why I don't write the command directly. It's just that the commands are in the form of a string from, let's say a database, & I need to run these commands. Note that other programming software does allow the programmer to do this.

Hope somebody can help! Thanks in advance.


Sanjeev Chakravarty

Posts: 2
Nickname: sanjeevc
Registered: May, 2004

Re: Exec. command in form of string Posted: May 24, 2004 2:12 PM
Reply to this message Reply
The command string you supply to the exec() is the same as the OS command, e.g., dir

Below are links for further reference:

http://www.javaworld.com/javatips/jw-javatip66_p.html http://javaalmanac.com/egs/java.lang/Exec.html http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20966148.html

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Exec. command in form of string Posted: May 25, 2004 2:27 AM
Reply to this message Reply
There are no "commands" to be called in java.

What you search, is "Call Method By Name".

I don't know if there exists something like that, but this term should help you search for it.


Runtime.exec() calls external programs (see API), not Java Methods.
The Compiler class has nothing to do with your problem (as I understood reading the API documentation).

Flat View: This topic has 2 replies on 1 page
Topic: cocoon Previous Topic   Next Topic Topic: Saving files

Sponsored Links



Google
  Web Artima.com   

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