Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Using JAVA to access System Resources

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Using JAVA to access System Resources

    I have sort of a theory that I was curious about before I attempted to do anything in practice.

    Is JAVA capable of going through a Windows system and collecting a list of all the programs install on the computer?

    Basically, I am hoping to build a program that will preserve the integrity of my family's new Windows 7 OS. The problem is my parents don't pay attention when they install things and end up with a bunch of Opt-Out crap installed on the computer, obviously doing long-term damage. My plan is to build a simple interface that will just have a list of the programs on the computer in a JTable or something. The user will be able to select programs as "intended" or something. Then a report will be made of the programs that the user said are not "intended" to be on the machine so the user can easily go through the Windows Programs And Features Uninstall processes and remove the programs in the report. Another report will be made of the programs the user flagged as "intended" and the next time the JAVA Application is ran those programs will already be flagged as "intended". If possible, browser plug-ins would also be nice to include, but that is not a priority at the moment.

    Fairly basic and will probably do a lot to save our computer in the long run. I figure if we run it once a month than we will have less crap weighing our computer down. The only problem I have is I am unsure how to get a list of programs that are installed on the computer. Does anyone know of a way to do that?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using JAVA to access System Resources

    I think I may have found a go-around for the restrictions.

    My plan is to use the command prompt to get a list of the programs, then read that list. To do that, I figure the easiest way would be to turn the list of programs into a txt file, and then read that txt file.

    I am having problems figuring out how to run the Command Prompt commands from JAVA.

    Based on a website I have found (and tested using the Command Prompt), these two commands will get you a list of the programs on your computer:
    C:\Windows\system32>wmic
    wmic:root\cli>/output:C:\Users\*USERNAME*\Desktop\InstallList.txt product get name,version
    where *USERNAME* is a variable for the Username.

    Source: How To: Get List Of Installed Apps In Vista/Windows 7 Without Any Software

    So I attempted to use my extremely limited and almost nonexistent knowledge of JAVA's Process class and needless to say that didn't go as perfectly as hoped.

    Here is the test program I made(also where *USERNAME* is a variable for the Username):
    import java.io.*;
    import java.util.*;
     
    public class ProgramFinder 
    {
     
        public static void main(String[] args) 
        {
        	try{
        	Runtime runtime = Runtime.getRuntime();
           	Process process = runtime.exec("wmic");
           	Process process2 = runtime.exec("/output:C:\\Users\\*USERNAME*\\Desktop\\InstallList.txt product get name,version");
     
        	}catch(Exception ex){System.out.println("Error: "+ex);    	}
        }
    }

    Upon execution, I get this runtime error:
    Error: java.io.IOException: Cannot run program "/output:C:\Users\*USERNAME*\Desktop\InstallList.txt ": CreateProcess error=2, The system cannot find the file specified
    Obviously, this is the fault of my lack of knowledge. Can someone help me get the command to run?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using JAVA to access System Resources

    When you get the first process, you should retrieve the InputSteam, OutputStream, and ErrorStream, using these to read and write to that process. Ideally do all of these in different threads...see the following article for some code on how to accomplish this: When Runtime.exec() won't

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using JAVA to access System Resources

    Quote Originally Posted by copeg View Post
    When you get the first process, you should retrieve the InputSteam, OutputStream, and ErrorStream, using these to read and write to that process. Ideally do all of these in different threads...see the following article for some code on how to accomplish this: When Runtime.exec() won't
    Ok, I'm a little confused about the whole thing. What does the InputStream, OutputStream, and ErrorStream do for me? It seems the examples on the page just use them to track the what is happening but other than that they do nothing. I can't seem to figure out where they are creating commands either.The best I could find was the accessing of the args[], which doesn't do me a lot of good when I have a specific command I want to hardcode. To my understanding, the examples are operating on the assumption that the user has used the command prompt to execute the program, thus args arguments existing. For example, in the GoodWinRedirect example on the link you provided, if there are no args arguments, it prompts the user with the intentions of the program and then just exits. If I have no args arguments, I assume I would have to hardcode the FileOutputStream. That just seems to conflict with the intention of the second command I want to run: wmic:root\cli>/output:C:\Users\*USERNAME*\Desktop\InstallList.txt product get name,version since that command indicates the file to be written to.

    I think I'm far more confused than you are giving me credit for.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using JAVA to access System Resources

    The link I gave demonstrates how you can run a command line tool, and read-to/write-from that tool while it is running - essentially it allows communication between a task that requires input (and typically which you would like to retrieve the output from). Calling the exec() function will start a new task, but unless that task ends immediately doing what you wish it to do, you must somehow manipulate the communication to tell the task to do something (and get the output as well). I do not have experience with wmic, but it seems this is what you wish to accomplish - start the task - then tell it to do something. There may be other arguments you can pass to the tool when you actually call it so as to not have to go the whole Runnable route, but again I don't have experience with that tool to say its possible.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using JAVA to access System Resources

    If you know what commands you want run, you can try using the code I have here: http://www.javaprogrammingforums.com...html#post21660. It's a little dirty but it will get the job done.

    Instead of what I did with using a StreamGobbler to print out any command prompt outputs, you can save this into a list in your program and then systematically examine this list.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    aussiemcgr (December 3rd, 2010)

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using JAVA to access System Resources

    That was a lot of help and I thought that I might have started to understand it, but I hit a snag and I need some explanation.

    Ok, so I used your code (I hope you don't mind, it is not like I am distributing it or anything) and edited it a bit to fit my needs:

    MAIN:
    import java.io.*;
    import java.util.*;
     
    public class ProgramFinder 
    {
     
        public static void main(String[] args) 
        {
        	try{
     
           	new ExeClass();
     
        	}catch(Exception ex){System.out.println("Error: "+ex);    	}
        }
    }
    PROCEDURE CLASS:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
     
    public class ExeClass 
    {
    	private static Process      cmd;
        private static InputStream  cmdOutput;
        private static PrintStream  cmdInput;
     
        public ExeClass() throws Exception
        {
        	cmd = Runtime.getRuntime().exec("cmd");
            cmdOutput = new BufferedInputStream(cmd.getInputStream());
            cmdInput = new PrintStream(new BufferedOutputStream(cmd.getOutputStream()));
            Thread display = new Thread(new StreamGobbler(cmdOutput, ""));
            display.setDaemon(true);
            display.start();
            runWndCommand("wmic");
            runWndCommand("/output:C:\\Users\\*USERNAME*\\Desktop\\InstallList.txt product get name,version");
            runWndCommand("EXIT");
            cmd.waitFor();
        }
        public void runWndCommand(String cmd) throws IOException, InterruptedException
        {
            cmdInput.println(cmd);
            cmdInput.flush();
        }
     
        class StreamGobbler extends Thread
    	{
    	    InputStream    is;
    	    String        type;
     
    	    StreamGobbler(InputStream is, String type)
    	    {
    	        this.is = is;
    	        this.type = type;
    	    }
     
    	    @Override
    	    public void run()
    	    {
    	        try
    	        {
    	            InputStreamReader isr = new InputStreamReader(is);
    	            BufferedReader br = new BufferedReader(isr);
    	            String line = null;
    	            while ((line = br.readLine()) != null)
    	            {
    	                System.out.println(type + ">" + line);
    	            }
    	        }
    	        catch (IOException ioe)
    	        {
    	            ioe.printStackTrace();
    	        }
    	    }
    	}
    }

    Now, it would appear that the wmic command works fine, and it would appear that most of the other command works. When I run the /output:C:\\Users\\*USERNAME*\\Desktop\\InstallList .txt product get name,version command, it goes as far as creating the InstallList.txt in the correct location, but then just hanges there and I am unsure if it is working or not. I let it hang there for 4 minutes before canceling the program. After canceling, I checked the text file and it is just empty.

    The output I get is:
    >Microsoft Windows [Version 6.1.7600]
    >Copyright (c) 2009 Microsoft Corporation. All rights reserved.
    >
    >C:\Users\*USERNAME*\Documents\JCreator LE\MyProjects\ProgramFinder\classes>wmic
    What reasons would there be for this to be happening?

    Also, in order to store the output to a list, would I have to redirect the output or how does that work?
    Last edited by aussiemcgr; December 3rd, 2010 at 02:33 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using JAVA to access System Resources

    I did a little reading into the wmic command you're using and you can have it output that information into the standard output instead of a file.

    It will take a while (more time if you have more programs installed or a slower computer), it took me maybe a minute or two and I have almost no programs installed on a decently fast computer.

    Try these commands:
            runWndCommand("wmic /output:STDOUT product get name,version");
            runWndCommand("EXIT");

    You can then capture all output from the output of the process (using the getInputStream() command )

  10. The Following User Says Thank You to helloworld922 For This Useful Post:

    aussiemcgr (December 3rd, 2010)

  11. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Using JAVA to access System Resources

    Quote Originally Posted by helloworld922 View Post
    I did a little reading into the wmic command you're using and you can have it output that information into the standard output instead of a file.

    It will take a while (more time if you have more programs installed or a slower computer), it took me maybe a minute or two and I have almost no programs installed on a decently fast computer.

    Try these commands:
            runWndCommand("wmic /output:STDOUT product get name,version");
            runWndCommand("EXIT");

    You can then capture all output from the output of the process (using the getInputStream() command )
    Ok, I have done all of that and now there is a snag sort of unrelated to the command line.

    I am calling this class from another class (to run a process, produce a list, and then send the list back to the main) but after it stores all of the programs in the Vector (and prints out the Vector afterwards) the rest of the program doesn't run. I think it is the thread still waiting, but I'm not sure. Any thoughts on what would cause this and how to fix it?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  12. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using JAVA to access System Resources

    Sounds like you have some deadlocking issues. Could you post your code so we can take a look at it?

Similar Threads

  1. Access jar's resources within a servlet
    By RogerBotter in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 10:18 AM
  2. Advice on Java personal msg system?
    By cyborgbill in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2010, 03:00 AM
  3. How do I access SENS information in JAVA?
    By cabodge in forum Java Theory & Questions
    Replies: 4
    Last Post: March 9th, 2010, 06:52 PM
  4. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM
  5. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM