Soul Enrapturer
Soul Enrapturer

Reputation: 377

Can I execute a cmd command through java?

I am making a program that needs some of the Java libraries installed already into the computer and also the "classpath" environment variable to be set.

I want to run the set classpath command. Can I do it through java? Or do I need to do something else? Any Example?

Upvotes: 0

Views: 149

Answers (5)

acostache
acostache

Reputation: 2275

Something like

public static void main(String[] args)
{
    try
    {
        if (args == null || (args != null && args.length != 1)) 
        {
        System.out.println("Please provide a command");
        }
        Runtime.getRuntime().exec(args);
    } 
    catch (Exception ex) 
    {
        ex.printStackTrace();
    }
}

Upvotes: 2

duffymo
duffymo

Reputation: 308733

Yes, you can. Here's are some examples to show you how to do it:

http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

http://www.ehow.com/way_5660016_java-runtime-exec-tutorial.html

Upvotes: 2

Philipp
Philipp

Reputation: 69663

set.exe is a program like any other. You can start it with Runtime.exec().

Upvotes: 1

Matthijs Bierman
Matthijs Bierman

Reputation: 1759

If you want to set a system property, you can use System.setProperty(key,value).

Upvotes: 2

NiranjanBhat
NiranjanBhat

Reputation: 1832

All you need is ProcessBuilder

Upvotes: 2

Related Questions