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.

1

2 Answers 2

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

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

Comments

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

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.