使用java将应用程序添加到系统路径

时间:2014-03-31 13:48:39

标签: java

如何使用java将具有特定名称的应用程序设置为系统路径?

例如,如果我需要一些应用程序app.exe来运行某些东西(在app item.torun之类的控制台中,那么我想检查应用程序是否在系统路径中设置,如果没有 - 然后添加它(假设用户是admin。

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行任何申请: -

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

您可以使用特定路径向应用程序发送参数,例如 c:\ users \ admin \ vlc.exe。以便它将被执行..

答案 1 :(得分:1)

以下程序将检查应用程序您要运行的内容是否存在,如果不存在,您可以将应用程序位置分配给类路径,稍后您可以执行..

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

} 
  }
相关问题