-1

i do have a class CheckPrograminstallation(which is part of a eclipse plugin), with a method check, which checks whether a program is installed. It return true when installed and false otherwise.

public class CheckPrograminstallation{
    public static boolean check(String programname, String OsName)
            throws Exception {

        // Get installation path of programname
        String foundpath = "";
        String dirName = "";
        String line;
        String programpath = null;
        Process process = null;
        boolean IsInstalled = false;

        if (OsName.equals("Windows")) {
            try {
                // get Windows Directory first
                process = Runtime.getRuntime().exec("cmd /c echo %windir%");
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                // read from stream 
                if ((line = reader.readLine()) != null) {
                    foundpath = line.toString();

                    // cut off "\Windows" from the found path
                    int last = foundpath.lastIndexOf("\\");
                    dirName = foundpath.subSequence(0, last).toString();
                    process = null;

                    // get program installation path
                    process = Runtime.getRuntime().exec(
                            "cmd /c where /R " + dirName + " " + programname);

                    reader = new BufferedReader(new InputStreamReader(
                            process.getInputStream()));
                    if ((line = reader.readLine()) != null) {
                        programpath = line.toString();
                        System.out.println(programpath);
                        IsInstalled = true;

                    }
                }
            } catch (Exception e) {
                DO SOMETHING);
            }
        }

When i call the method from a test class, it works. But when i call the same method while running the Plugin:

...boolean isInstalledPscp;
   boolean IsWindows;
...
        if (IsWindows == true) {
            // for Windows: check if pscp is installed
            isInstalledPscp = CheckIfInstalled.check("pscp", "Windows");
        if (isInstalledPscp == false) {
            do something }
        }

...it always returns false. How can that be?

This has been driving me crazy for a whole day. Using .equals for String comparison, and still getting false as result. So this is not a string comparison problem IMHO.

Sandra
  • 103
  • 2
  • 14

3 Answers3

0

Change your string comparison from:

if (OsName == "Windows") {

To:

if (OsName.equals("Windows")) {

Since your if doesn't succeed, it never goes into if and hence it returns your false.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • fixed that, still returns false – Sandra Feb 24 '15 at 16:05
  • There could be multiple issues then. 1. code doesnt go in `if (IsWindows == true) {` and hence you land up with default value 2. your code doesnt go into `if ((line = reader.readLine()) != null) {` (note its twice) and to set your variable to true it should enter in both the loops. – SMA Feb 24 '15 at 16:40
0

You compare strings using the equals() method and not logical equals operator ==

I also recommend that you follow java naming conventions and use variables names starting with lower case letters such as osName instead of OsName.

steven35
  • 3,747
  • 3
  • 34
  • 48
0

Print out what are the paths that are returned inside your code (if any), I suspect that from within the plugin the runtime parameters are different.

Also maybe System.getEnv System.getProperties is a better way to find windows dir than starting a new process.

Zielu
  • 8,312
  • 4
  • 28
  • 41
  • problem is that due to a bug i cannot use Self Hosting in Eclipse and without Self Hosting in Debug mode i have no idea how to show console outputs. I basically i cannot see the outputs. I cannot System, getproperties because i determine OS from ontology information – Sandra Feb 24 '15 at 16:11
  • You can debug to a file. You are not using ontologies you are calling 'echo' and then 'where' from the command line. – Zielu Feb 24 '15 at 16:15
  • determining type of Os from an Ontology, using 'where' to find out drive name. – Sandra Feb 24 '15 at 16:22
  • Seriously, save your paths, and calls to the file and see what is happening. Again you are checking for existence of a file, just use File.exists for it. – Zielu Feb 24 '15 at 16:32