Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

688 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