1

Something equivalent to this command line:

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

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

EDIT:

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

vale4674
  • 3,931
  • 13
  • 41
  • 72
  • 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. – Usman Saleem Dec 02 '11 at 01: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. – Dave L. Dec 02 '11 at 01:31
  • @david The user would do this only once, the first time he start the application, not every time he starts it. – vale4674 Dec 02 '11 at 01:48
  • @Usman Saleem I edited the question. – vale4674 Dec 02 '11 at 01: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. http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java – Dave L. Dec 02 '11 at 01:53
  • @david That's the thing, it's not some configuration and program isn't in PATH, it is rather installation of my program. The libraries I am using expect VLC in PATH, so once it is in the PATH my application works like it is supposed to work. The best thing would be if VLC installation itself put it's folder in PATH. I'll go with Deco's answer below. Tnx for suggestions though. – vale4674 Dec 02 '11 at 14:44

1 Answers1

3

You can execute commands using the Process object, 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");
    }
}
Deco
  • 3,211
  • 14
  • 23
  • 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](http://stackoverflow.com/questions/8358265/how-to-update-path-variable-permanently-from-cmd-windows). – vale4674 Dec 02 '11 at 15:05
  • 1
    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. – Stephen C Jul 07 '19 at 01:54