lapots
lapots

Reputation: 13395

add application to system path using java

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.

Upvotes: 1

Views: 87

Answers (2)

Devavrata
Devavrata

Reputation: 1785

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..

Upvotes: 1

loknath
loknath

Reputation: 1372

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

} 
  }

Upvotes: 1

Related Questions