Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

static - List of useful environment settings in Java

I've been wondering a long time if there was a comprehensive list of (probably static) methods/fields that store runtime information for the JVM. An incomplete list of examples:

  • System.out / System.in
  • System.currentTimeMillis()
  • System.getProperty()
  • System.getConsole()
  • Runtime.freeMemory()
  • Etc

Does anyone have a link or something?

EDIT: I'm not so dumb as to have not checked the docs for System and Runtime :P I was just wondering if there were other classes where similar methods to determine the state of the machine you're running on are stored.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

General Properties

I use this code to get a handle on some of the things known to Java classes that are of particular interest to me.

System Properties

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;

import java.security.*;

/** A small GUId app. that shows many system and environment properties.
Designed to be compatible with Java 1.4+ (hence many requirements like
no foreach, no generics, no StringBuilder..).
@author Andrew Thompson
@version 2008-06-29
 */
class SystemProperties {

    static String sentence = "The quick brown fox jumped over the lazy dog.";
    static String sep = System.getProperty("line.separator");
    static String fontText =
            sentence +
            sep +
            sentence.toUpperCase() +
            sep +
            "0123456789 !@#$%^&*()_+ []\;',./ {}|:"<>?";

    static String[] convertObjectToSortedStringArray(Object[] unsorted) {
        String[] sorted = new String[unsorted.length];
        for (int ii = 0; ii < sorted.length; ii++) {
            sorted[ii] = (String) unsorted[ii];
        }
        Arrays.sort(sorted);
        return sorted;
    }

    static String dataPairToTableRow(String property, Object value) {
        String val = valueToString(property, value);
        return "<tr>" +
                "<th>" +
                "<code>" +
                property +
                "</code>" +
                "</th>" +
                "<td>" +
                val +
                "</td>" +
                "</tr>";
    }

    static String valueToString(String property, Object value) {
        if (value instanceof Color) {
            Color color = (Color) value;
            String converted =
                    "<div style='width: 100%; height: 100%; " +
                    "background-color: #" +
                    Integer.toHexString(color.getRed()) +
                    Integer.toHexString(color.getGreen()) +
                    Integer.toHexString(color.getBlue()) +
                    ";'>" +
                    value.toString() +
                    "</div>";
            return converted;
        } else if (property.toLowerCase().endsWith("path") ||
                property.toLowerCase().endsWith("dirs")) {
            return delimitedToHtmlList(
                    (String) value,
                    System.getProperty("path.separator"));
        } else {
            return value.toString();
        }
    }

    static String delimitedToHtmlList(String values, String delimiter) {
        String[] parts = values.split(delimiter);
        StringBuffer sb = new StringBuffer();
        sb.append("<ol>");
        for (int ii = 0; ii < parts.length; ii++) {
            sb.append("<li>");
            sb.append(parts[ii]);
            sb.append("</li>");
        }
        return sb.toString();
    }

    static Component getExampleOfFont(String fontFamily) {
        Font font = new Font(fontFamily, Font.PLAIN, 24);
        JTextArea ta = new JTextArea();
        ta.setFont(font);
        ta.setText(fontText);
        ta.setEditable(false);
        // don't allow these to get focus, as it
        // interferes with desired scroll behavior
        ta.setFocusable(false);
        return ta;
    }

    static public JScrollPane getOutputWidgetForContent(String content) {
        JEditorPane op = new JEditorPane();
        op.setContentType("text/html");
        op.setEditable(false);

        op.setText(content);

        return new JScrollPane(op);
    }

    public static void main(String[] args) {
        JTabbedPane tabPane = new JTabbedPane();
        StringBuffer sb;
        String header = "<html><body><table border=1 width=100%>";

        sb = new StringBuffer(header);
        Properties prop = System.getProperties();
        String[] propStrings = convertObjectToSortedStringArray(
                prop.stringPropertyNames().toArray());
        for (int ii = 0; ii < propStrings.length; ii++) {
            sb.append(
                    dataPairToTableRow(
                    propStrings[ii],
                    System.getProperty(propStrings[ii])));
        }
        tabPane.addTab(
                "System",
                getOutputWidgetForContent(sb.toString()));

        sb = new StringBuffer(header);
        Map environment = System.getenv();
        String[] envStrings = convertObjectToSortedStringArray(
                environment.keySet().toArray());
        for (int ii = 0; ii < envStrings.length; ii++) {
            sb.append(
                    dataPairToTableRow(
                    envStrings[ii],
                    environment.get(envStrings[ii])));
        }
        tabPane.addTab(
                "Environment",
                getOutputWidgetForContent(sb.toString()));

        sb = new StringBuffer(header);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        for (int j = 0; j < gs.length; j++) {
            GraphicsDevice gd = gs[j];
            sb.append(
                    dataPairToTableRow(
                    "Device " + j,
                    gd.toString() +
                    "  " +
                    gd.getIDstring()));
            GraphicsConfiguration[] gc =
                    gd.getConfigurations();
            for (int i = 0; i < gc.length; i++) {
                sb.append(
                        dataPairToTableRow(
                        "Config " +
                        i,
                        (int) gc[i].getBounds().getWidth() +
                        "x" +
                        (int) gc[i].getBounds().getHeight() +
                        " " +
                        gc[i].getColorModel() +
                        ", " +
                        "  Accelerated: " +
                        gc[i].getImageCapabilities().isAccelerated() +
                        "  True Volatile: " +
                        gc[i].getImageCapabilities().isTrueVolatile()));
            }
        }
        tabPane.addTab(
                "Graphics Environment",
                getOutputWidgetForContent(sb.toString()));

        String[] fonts = ge.getAvailableFontFamilyNames();
        JPanel fontTable = new JPanel(new BorderLayout(3, 1));
        // to enable key based scrolling in the font panel
        fontTable.setFocusable(true);
        JPanel fontNameCol = new JPanel(new GridLayout(0, 1, 2, 2));
        JPanel fontExampleCol = new JPanel(new GridLayout(0, 1, 2, 2));
        fontTable.add(fontNameCol, BorderLayout.WEST);
        fontTable.add(fontExampleCol, BorderLayout.CENTER);
        for (int ii = 0; ii < fonts.length; ii++) {
            fontNameCol.add(new JLabel(fonts[ii]));
            fontExampleCol.add(getExampleOfFont(fonts[ii]));
        }
        tabPane.add("Fonts", new JScrollPane(fontTable));

        sb = new StringBuffer(header);

        sb.append("<thead>");
        sb.append("<tr>");
        sb.append("<th>");
        sb.append("Code");
        sb.append("</th>");
        sb.append("<th>");
        sb.append("Language");
        sb.append("</th>");
        sb.append("<th>");
        sb.append("Country");
        sb.append("</th>");
        sb.append("<th>");
        sb.append("Variant");
        sb.append("</th>");
        sb.append("</tr>");
        sb.append("</thead>");

        Locale[] locales = Locale.getAvailableLocales();
        SortableLocale[] sortableLocale = new SortableLocale[locales.length];
        for (int ii = 0; ii < locales.length; ii++) {
            sortableLocale[ii] = new SortableLocale(locales[ii]);
        }
        Arrays.sort(sortableLocale);
        for (int ii = 0; ii < locales.length; ii++) {
            String prefix = "";
            String suffix = "";
            Locale locale = sortableLocale[ii].getLocale();
            if (locale.equals(Locale.getDefault())) {
                prefix = "<b>";
                suffix = "</b>";
            }
            sb.append(dataPairToTableRow(
                    prefix +
                    locale.toString() +
                    suffix,
                    prefix +
                    locale.getDisplayLanguage() +
                    suffix +
                    "</td><td>" +
                    prefix +
                    locale.getDisplayCountry() +
                    suffix +
                    "</td><td>" +
                    prefix +
                    locale.getDisplayVariant() +
                    suffix));
        }
        tabPane.add("Locales",
                getOutputWidgetForContent(sb.toString()));

        Locale.getDefault();
        int border = 5;
        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(new EmptyBorder(border, border, border, border));
        p.add(tabPane, BorderLayout.CENTER);
        p.setPreferredSize(new Dimension(400, 400));
        JFrame f = new JFrame("Properties");
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.getContentPane().add(p, BorderLayout.CENTER);
        f.pack();
        f.setMinimumSize(f.getPreferredSize());
        f.setSize(600, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class SortableLocale implements Comparable {

    Locale locale;

    SortableLocale(Locale locale) {
        this.locale = locale;
    }

    public String toString() {
        return locale.toString();
    }

    public Locale getLocale() {
        return locale;
    }

    public int compareTo(Object object2) {
        SortableLocale locale2 = (SortableLocale) object2;
        //Locale locale2 = (Locale)object2;
        return locale.toString().compareTo(
                locale2.toString());
    }
}

Media

Properties related to synthesized and sampled sound, and images.

Media Types

/*
<applet
    code='MediaTypes'
    width='900'
    height='600'>
<param name='show' value='Sound|Sampled|Mixers|Primary Sound Capture Driver'>
</applet>
*/
import javax.imageio.ImageIO;
import javax.sound.sampled.*;
import javax.sound.midi.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.text.Position;

public class MediaTypes extends JApplet {

    JTable table;
    boolean sortable = false;
    JTree tree;

    @Override
    public void init() {
        Runnable r = () -> {
            MediaTypes mediaTypes = new MediaTypes();

            String show = "";
            if (getParameter("show")!=null) {
                show = getParameter("show");
            }

            JPanel p = new JPanel();
            mediaTypes.createGui(p, show);
            add(p);
            validate();
        };
        SwingUtilities.invokeLater(r);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            MediaTypes mediaTypes = new MediaTypes();

            JPanel p = new JPanel();
            mediaTypes.createGui(p);
            JOptionPane.showMessageDialog(null,p);
        };
        SwingUtilities.invokeLater(r);
    }

    public Object[][] mergeArrays(String name1, Object[] data1, String name2, Object[] data2) {
        Objec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...