0

I am getting hardware data from cmd using a process builder in java.

//get info from cmd
    ProcessBuilder processBuilder = new ProcessBuilder();

        processBuilder.command("cmd.exe", "/c", "systeminfo ");

        try {

            Process process = processBuilder.start();

            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(process.getInputStream()));
            //write the output to the variables
            String line;
            //a loop which ensures all the data is read in
            while ((line = reader.readLine()) != null) {
                hardwareInfo.add(line);//hardwareInfo is an arraylist i currently save all this information to
            }

This returns all the relevant information and more. my output looks like the following:

[, Host Name:                 LJDESKTOP, OS Name:                   Microsoft Windows 10 Education, OS Version:                10.0.18363 N/A Build 18363

etc...

I want to add some of these fields into an SQL database based on their names(e.g. Host Name: - yes, OS Name: - No). The SQL connection is set up I just need to find the best way to save these varibles so I can insert them straight into my database.

So how do I get rid of the Host Name: and still enter LJDESKTOP into my database and the same principle for the rest of the information I get from the cmd command.

I am also trying to consider efficiency, I want this to be as computationally "light" as possible but this isn't essential.

What I have tried so far:

  1. I have tried splitting the string at the ":" for each varible and trimming. This gives me exactly the information I need but then I can't save it to individual variables. This is because the bit I trimmed is how I determine what my varibles are. (Could I potentially add the trim function to my setter?)

  2. I have tried:

    while ((line = reader.readLine()) != null) {

        if (line.startsWith("Host Name:")) {
            setHostname(line.replaceFirst("Host Name: ", ""));}
    

the if statements are repeated for each variable, however everytime this adds each variable to my array everytime it goes through the while loop.

LiamWBA
  • 53
  • 7
  • 1
    So what have you tried so far? – Scary Wombat Dec 22 '20 at 01:45
  • Does this answer your question? [How to get the insert ID in JDBC?](https://stackoverflow.com/questions/1915166/how-to-get-the-insert-id-in-jdbc) –  Dec 22 '20 at 01:47
  • Looks like it could use some string parsing. Drop the '[', split string by comma to get ` Host Name: LJDESKTOP`, split by ':' to further get pairs of " Host Name"," LJDESKTOP", strip the strings to get rid of whitespaces before and after, and you will end up with "Host Name","LJDESKTOP". At that point, you'll be able to compare your keys(like "Host name") and figure out where to place to value(like "LJDESKTOP") in the JDBC calls. – vc669 Dec 22 '20 at 02:07
  • @SaahilMalhotra No I don't believe this answers my question as i am not getting the information from my database. I am getting the information from my pc and then I want to input into my database depending on the field name cmd returns. – LiamWBA Dec 22 '20 at 02:16
  • @vc669 sorry I am probably really bad at explaining this but yes I think this is along the right lines of what I beleive the solution to this is but I don't know the code for it. If you can give me a few minutes I will post what I have tried so far. Also I need to save it as a variable before I do this which adds extra complexity i can't get my head around... save LJDESKTOP as a variable 'hostname' (without "Host Name:") then input that variable's value to my database. – LiamWBA Dec 22 '20 at 02:22
  • @ScaryWombat I will edit my post to tell you what I have tried so far. Give me a few minutes. – LiamWBA Dec 22 '20 at 02:24
  • @LiamWBA you don't need to store it in a variable called hostname. Here's some pseudocode, you can instead simply do something like `if( key matches "Host name:") db.put("host_name",value)` depending on your actual variables. In practice, this might go in a `switch`. – vc669 Dec 22 '20 at 02:45
  • Your tag for `processbuilder` and the first half of your Question are irrelevant. When posting here, put some effort into paring down your question to the bare minimum needed to express your issue. – Basil Bourque Dec 22 '20 at 06:47
  • Duplicate of: [*Java parsing a string with lots of whitespace*](https://stackoverflow.com/q/9284466/642706) – Basil Bourque Dec 22 '20 at 06:49
  • @BasilBourque I had already tried that for some reason it wasn't recognising the gaps as whitespace. I think this is due to the format that the CMD outputs. Therefore, I thought it would be relevant to include the process builder so you knew where I was getting the information from. – LiamWBA Dec 22 '20 at 12:33

1 Answers1

1

You can try it like this:

...
final Map<String,String> inputValues = new HashMap<>();

//a loop which ensures all the data is read in
while ((line = reader.readLine()) != null) {
  // Read each line as key and value into a the inputValues map

  final String[] pieces = line.split(":",2); // only split at the first ':'!

  // Was a ':' found, e.g. the string split into two pieces?
  if ( pieces.length > 1 ) {

    String key = pieces[0]; // e.g. "Host Name"
    String value = pieces[1]; // e.g. "                 LJDESKTOP"

    value = value.trim(); // remove leading/trailing whitespaces from value

    inputValues.put(key,value); // Store key+value to map.
  }
}

// Now we can access the input values by key, e.g.:

String hostName = inputValues.get("Host Name");
String osName = inputValues.get("OS Name");

// To do: Do something with the values above, e.g. send to DB...
JimmyB
  • 12,101
  • 2
  • 28
  • 44