Thursday, July 28, 2016

Solved - Error occurred during initialization of VM; java/lang/NoClassDefFoundError: java/lang/Object

After a new installation of java, occasionally it throws this error when running java.

Following is the procedure to fix it.

 Run the command: $JAVA_HOME/bin/java

1. If it returns the usage instruction of java,

            Usage: java [-options] class [args...]
                               (to execute a class)
                   or  java [-options] -jar jarfile [args...]
                               (to execute a jar file)
             ........

       you have installed java correctly.
      If you have multiple versions of java installed on your machine, run command:

                $JAVA_HOME/bin/java -version

       to check if the correct version is executed. If not, correct your PATH to ensure the correct version of java is used.

2. If executing the command "$JAVA_HOME/bin/java" gives the error:

             Error occurred during initialization of VM
             java/lang/NoClassDefFoundError: java/lang/Object

     it means java is not properly installed. You can reinstall java or do the following.

      Check if you can find any .pack files (e.g tools.pack and rt.pack) in the $JAVA_HOME/lib, $JAVA_HOME/jre/lib, and $JAVA_HOME/jre/ext directores. If such files exist, unpack all of them to .jar files with the following command.

            $JAVA_HOME/bin/unpack200 -r -v -l "" <file>.pack <file>.jar

          e.g. $JAVA_HOME/bin/unpack200 -r -v -l "" $JAVA_HOME/lib/tools.pack $JAVA_HOME/lib/tools.jar

---------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Wednesday, July 27, 2016

Solved - JTable: wrap text in a cell, column, or row

To wrap text in a JTable, you need to create a TableCellRenderer and TableCellEditor that use the JTextArea as the component. Then, use them for the cell, column, row, or the whole table where you want to wrap text. Following is a sample code.


public class TableWrapText extends JFrame {
    private DefaultTableModel tableModel = null;
 
    public TableWrapText() {
        String[][] data = new String[][]{
            new String[]{"Travel", "Go to somewhere else from where you live to enjoy spending money and getting lost in the esoterics."},
            new String[]{"Help", "Do something to make the person get what he or she wants for pay or for free, it is upto you."},
            new String[]{"Sleep", "A state of temperary unconscious due to the nerves system is busy with something else."}
        };
        tableModel = new DefaultTableModel(data, new String[]{"Name", "Content"});

        JTable table = new JTable(tableModel){
            public TableCellRenderer getCellRenderer(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyRenderer();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyRenderer();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyRenderer();
//              }    
                } else {
                    return new DefaultTableCellRenderer();
                }
            }

            public TableCellEditor getCellEditor(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyEditor();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyEditor();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyEditor();
//              }    
                } else {
                    return new DefaultCellEditor(new JTextField());
                }
            }
        };
        table.setRowHeight(0, table.getFont().getSize()*3);
//        table.setRowHeight(table.getFont().getSize()*3);
        JScrollPane sp = new JScrollPane(table);
        getContentPane().add(sp);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 400);
        this.setVisible(true);
    }
 
    private class MyRenderer extends JTextArea implements TableCellRenderer{
        public MyRenderer() {
            setOpaque(true);
            setLineWrap(true);
            setWrapStyleWord(true);
        }
     
        public Component getTableCellRendererComponent(JTable table,Object value,
            boolean isSelected, boolean hasFocus, int row,int column) {
         
            this.setText(value == null ? "" : value.toString());
            return this;
        }

    }

    private class MyEditor extends AbstractCellEditor implements TableCellEditor {
        JTextArea comp = new JTextArea();
        JTable table;
        int row;
     
        public MyEditor() {
            comp.setLineWrap(true);
            comp.setWrapStyleWord(true);
            comp.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    super.componentResized(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
            comp.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    super.keyTyped(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            this.table = table;
            this.row = row;

            comp.setText((String) value);
            comp.setFont(table.getFont());

            return comp;
        }

        public Object getCellEditorValue() {
            return comp.getText();
        }
    }
 
    public static void main(String[] args){
        new TableWrapText();
    }
}

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.