2

Something equivalent to this command line:

set PATH=%PATH%;C:\Something\bin

To run my application, something has to be in a PATH variable. So I want at the program beginning catch exceptions if the program fails to start and display some wizard for the user to select the installation folder of a program that needs to be in a PATH. Then I would take that folder's absolute path and add it to the PATH variable and start my application again.

That "something" is VLC media player. I need its installation folder in the PATH variable (for example: C:\Program Files\VideoLAN\VLC). My application is a single executable .jar file and in order to use it, VLC needs to be in a PATH. So when the user first starts my application, that little wizard would pop up to select the VLC folder, and then I would update PATH with it.

6
  • That "something" is independent of your program? I would use a conf/properties file that i can utilize from my .bat to append to PATH before launching the program. Take a look at any open source application server's .bat files such as JBoss etc. on how to achieve this. Commented Dec 2, 2011 at 1:30
  • 4
    If the user is basically going to enter the path to the program you need, why do you need to modify the PATH variable? You only need to modify the PATH if you don't know the absolute path to the program you need. Commented Dec 2, 2011 at 1:31
  • @david The user would do this only once, the first time he start the application, not every time he starts it. Commented Dec 2, 2011 at 1:48
  • @Usman Saleem I edited the question. Commented Dec 2, 2011 at 1:48
  • 3
    @vale4674 -- In my opinion it would be better to save the user's input to a configuration file. If the program is already on the PATH, you can still get its absolute path and save that to the conf file. stackoverflow.com/questions/318239/… Commented Dec 2, 2011 at 1:53

1 Answer 1

3

You can execute commands using the Process object, and you can also read the output of that using a BufferedReader. Here's a quick example that may help you out:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String args[]) {
        try {
            Process proc = Runtime.getRuntime().exec("cmd set PATH=%PATH%;C:\\Something\\bin");
            proc.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

            String line = reader.readLine();
            while (line != null) {
                // Handle what you want it to do here
                line = reader.readLine();
            }
        }
        catch (IOException e1) {
            // Handle your exception here
        }
        catch(InterruptedException e2) {
            // Handle your exception here
        }

        System.out.println("Path has been changed");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This looks like the thing I need for executing cmd commands from java application but now I see that I have another problem. I asked another question.
Yes. This doesn't actually solve the OP's problem. In particular cmd set ... launches a new command shell and sets the environment variable in it. Then the shell exits. You need to use setx instead of set ... but that doesn't change the setting for existing shells.
accepted but having a new problem @vale4674 ? what's the next problem anyway...?

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.