Java File Attribute findUnixExecutable(String unixCommandLineExecutables)

Here you can find the source of findUnixExecutable(String unixCommandLineExecutables)

Description

find Unix Executable

License

Open Source License

Parameter

Parameter Description
unixCommandLineExecutables a parameter

Return

the complete path of the executable otherwise it will return xpce

Declaration

private static String findUnixExecutable(String unixCommandLineExecutables) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * This file is part of the Prolog Development Tool (PDT)
 * //from  w  ww  .ja v  a 2  s. co  m
 * Author: Lukas Degener (among others)
 * WWW: http://sewiki.iai.uni-bonn.de/research/pdt/start
 * Mail: pdt@lists.iai.uni-bonn.de
 * Copyright (C): 2004-2012, CS Dept. III, University of Bonn
 * 
 * All rights reserved. This program is  made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 ****************************************************************************/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Collection;

import java.util.Vector;

public class Main {
    /**
     * @author Hasan Abdel Halim
     * 
     * Finds the current SWI-Prolog executable for UNIX/BSD-BASED OS
     * @param unixCommandLineExecutables 
     * @return the complete path of the executable otherwise it will return xpce
     */
    private static String findUnixExecutable(String unixCommandLineExecutables) {
        String[] default_exec = unixCommandLineExecutables.split(",");

        // TODO shall we look for the env. variables as we do for Windows ?
        String[] appendPath = null;

        // Hack to resolve the issue of locating xpce in MacOS
        if (isMacOS()) {
            appendPath = new String[1];
            appendPath[0] = "PATH=$PATH:" + System.getProperty("user.home") + "/bin:/opt/local/bin";
        }

        try {

            for (String exec : default_exec) {

                Process process = Runtime.getRuntime().exec("which " + exec, appendPath);

                if (process == null)
                    return null;

                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String path = br.readLine();

                if (path == null || path.startsWith("no " + default_exec))
                    continue;
                else {
                    return path;
                }
            }
            return default_exec[0];

        } catch (IOException e) {

            return default_exec[0];
        }
    }

    public static void split(String string, String search, Collection<String> results) {
        if (string == null) {
            return;
        }
        int i = -1;
        while ((i = string.indexOf(search, 0)) >= 0) {
            results.add(string.substring(0, i).trim());
            string = string.substring(i + search.length());
        }
        String rest = string.trim();
        if (rest.length() > 0) {
            results.add(rest);
        }

    }

    public static String[] split(String string, String search) {
        Vector<String> v = new Vector<String>();
        split(string, search, v);
        return v.toArray(new String[v.size()]);

    }

    /**
     * @return
     */
    public static boolean isMacOS() {
        boolean mac = System.getProperty("os.name").indexOf("Mac") > -1;
        return mac;
    }
}

Related

  1. ensureFileIsExecutable(String filename)
  2. fileExecute(String path)
  3. findInPath(String executable, String path, String pathSeparator)
  4. findJavaCompilerExecutableInDir(File dir)
  5. findJavaExecutable(File vmInstallLocation)
  6. javaExecutable(File jre)
  7. launchExecutable(String executable, List args, Preferences preferences)
  8. makeCurrentDirectoryThatOfExecutable(String executable)
  9. makeExecutable(File file)