1

How to set application with certain name to system path using java?

For example if I need some application app.exe in order to run something(in console like app item.torun , then I want to check whether the application set in system path and if not - then add it(it packaged with application) assuming user is admin.

lapots
  • 9,619
  • 22
  • 96
  • 201
  • 1
    This will help for Windows: http://stackoverflow.com/questions/8350663/how-can-i-set-update-path-variable-from-within-java-application-on-windows – Kon Mar 31 '14 at 13:50

2 Answers2

1

You can execute any application by:-

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
class Main
{
public static void main(String[] args)throws IOException
{
    Process pr=Runtime.getRuntime().exec(args[0]);
    InputStreamReader isr=new InputStreamReader(pr.getInputStream());
    BufferedReader br=new BufferedReader(isr);
    String line="";
    while((line=br.readLine())!=null)
    {
        System.out.println(line);
    }
}
}

you can send argument to the application with specific path like c:\users\admin\vlc.exe. so it that it ll get executed..

loknath
  • 1,322
  • 15
  • 24
Devavrata
  • 1,655
  • 13
  • 26
1

Below program will check The application what you want to run is exist or not if not you can assigned application location to the class path and later you can execute ..

 package com.loknath.lab;

 import java.io.File;
 import java.io.IOException;

public class Demo {
public static void main(String[] args) throws IOException,
        InterruptedException {

    String applicationName = "x";
    String key = "x", value = "c//as//xyx.exe";

    File application = new File(applicationName);

    if (application.exists()) {

        Process p = Runtime.getRuntime().exec(application.toString());
        p.waitFor();

    } else {

        System.setProperty(key, value);
        System.out.println(System.getenv("CLASSPATH"));
    }

} 
  }
loknath
  • 1,322
  • 15
  • 24