0

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?

Atropo
  • 12,231
  • 6
  • 49
  • 62
Soul Enrapturer
  • 367
  • 2
  • 3
  • 14

5 Answers5

2

All you need is ProcessBuilder

NiranjanBhat
  • 1,812
  • 13
  • 17
2

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

Matthijs Bierman
  • 1,720
  • 9
  • 14
  • I think that was what I needed-So I know now that Environment variables can be changed without running cmd command in Java.Thanks – Soul Enrapturer Dec 19 '12 at 05:08
  • Just know that these properties are *not* global. See this answer: http://stackoverflow.com/a/908965/226449 – Matthijs Bierman Dec 19 '12 at 08:55
  • Do you mean the environment variable that i have changed won't have a changed value in another program that I later or in parallel run on that machine?What about if I have multiple threads in a process?Will it be working there too? – Soul Enrapturer Dec 20 '12 at 06:27
  • Another JVM/process will not have the changed value, correct. Within the same process (i.e. another thread) it will show the changed value. – Matthijs Bierman Dec 21 '12 at 15:57
2

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

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();
    }
}
acostache
  • 2,177
  • 7
  • 23
  • 48
1

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

Philipp
  • 67,764
  • 9
  • 118
  • 153