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?

2
  • 1
    You can use Runtime.getRuntime().exec("<command>") to execute commandline commands. But i'm not sure if this is really what you want to do.. Commented Dec 18, 2012 at 10:49
  • try java -classpath .;myjar.jar;lib/referenced-class.jar my.package.MainClass Commented Dec 18, 2012 at 10:50

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

I think that was what I needed-So I know now that Environment variables can be changed without running cmd command in Java.Thanks
Just know that these properties are not global. See this answer: stackoverflow.com/a/908965/226449
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?
Another JVM/process will not have the changed value, correct. Within the same process (i.e. another thread) it will show the changed value.
2

All you need is ProcessBuilder

Comments

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

Comments

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.