Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create a java application for RedHat and run the DMIDecode to return the data

Posted on 2011-03-13
3
Medium Priority
?
1,552 Views
Last Modified: 2013-12-14
public class SerNum {

Could someone tell me what is wrong with the code below? I'm trying to create a java application that will run in RedHat. I would like to get the serial number of the system by using DMIDecode and return it as a string.


   


 public static String GetSerNum() {

        String[] command = { "dmidecode -s system-serial-number" };

        String sNum = null;
 

        try
         {

       Process SerNumProcess = Runtime.getRuntime().exec(command);

      BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));

            sNum = sNumReader.readLine().trim();

            SerNumProcess.waitFor();

            sNumReader.close();

        }

        catch (Exception ex)
        {
           

            sNum = "Did not work";

        }

        finally
        {

            return sNum;

        }

    }
0
Comment
Question by:bojeff30
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35125442
whats the current problem with it?
0
 
LVL 4

Expert Comment

by:subratabiswas
ID: 35125685
Following is the same code that you provided, just did some formatting, added the necessary imports and the main function, and (importantly) declared the variable 'command' as a string.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SerNum {
    public static String GetSerNum() {
        String command = "dmidecode -s system-serial-number";
        String sNum = null;
        try {  
            Process SerNumProcess = Runtime.getRuntime().exec(command);
            BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
            sNum = sNumReader.readLine().trim();
            SerNumProcess.waitFor();
            sNumReader.close();
        } catch (Exception ex) {
            sNum = "Did not work";
        } finally {
            return sNum;
        }      
    }
    public static void main(String argv[]) {
        SerNum s = new SerNum();
        System.out.println(s.GetSerNum());
    }
}



See below the feedback from compilation and execution:

$ javac SerNum.java
$ export CLASSPATH=.
$ java SerNum
Did not work
$ dmidecode -s system-serial-number
/dev/mem: Permission denied

Same steps executed as root


# cd /home/subrata/temp
# export CLASSPATH=.
# java SerNum
5Y2BKLMZM1GM
# dmidecode -s system-serial-number
5Y2BKLMZM1GM

As seen in the above, there are many possible issues:

1) imports were missing; this would result into compilation error
2) main function was missing, this would also result in /missing main function' complaint
3) The original did not have execute privilege on the command dmidecode
4) Declared command as a simple string, not as an array of strings. The way it was provided, the whole string, including the intended arguments, were being considered as the executable, whlie naturfally there is no executable by that long name.

0
 
LVL 4

Accepted Solution

by:
subratabiswas earned 2000 total points
ID: 35125864
To elaborate on 3) above, the original user that created the java program, compiled it and tried to execute it, did not have the necessary privilege to execute the underlying command dmidecode. On my test machine, this was a privileged command, so the command threw error message for non-privileged users. However, when the command was executed as root, it executed correctly, both when invoked from command prompt and indirectly from within  the java program.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question