0

Please help me to solve : how to run .reg file in java netbeans ? And how to set source file automatically if I open the program in different computer ?

I have an reg file like this :

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR]
"Start"=dword:00000004

And this is my Java Code :

/**
 * Creates new form Form_DisableEnable
 */
public Form_DisableEnable() {
    initComponents();

    Dimension x = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension y = getSize();
    setLocation(
    (x.width - y.width)/2,
    (x.height - y.height)/2);
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem2 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Disable/Enable USB");

    jButton1.setText("Disable");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("Enable");

    jLabel1.setText("IP Address");

    jMenu1.setText("File");

    jMenuItem2.setText("Sign out");
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItem2ActionPerformed(evt);
        }
    });
    jMenu1.add(jMenuItem2);

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Helps");
    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(37, 37, 37)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2))
                    .addComponent(jTextField1)))
            .addContainerGap(213, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(25, 25, 25)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(168, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    if(JOptionPane.showConfirmDialog(null,"Are You sure want to logout ? ","Caution",
            JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE)==JOptionPane.OK_OPTION) {
        LoginForm dexs=new LoginForm();
        dexs.setVisible(true);
        this.dispose();
    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /*
     * Set the Nimbus look and feel
     */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel. For details see
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Form_DisableEnable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Form_DisableEnable().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JTextField jTextField1;
// End of variables declaration}

Thanks in advance. Regards, David

2
  • Please clarify your question. Do you want to import a registry file from a java program ? Commented May 16, 2012 at 2:29
  • Dear Max, now I already add file .reg file into java package and now how can I call that file using button ? Commented May 16, 2012 at 2:32

1 Answer 1

1

Try this to import registry file:

Desktop.getDesktop().open(new File("c:/file.reg"));

If the file is in a jar, then you would probably need to write it to a temporary file and open that file instead.

EDIT:

Try something similar to the following snippet to load registry file, copy it to a temp file and then open temp file using default registry editor:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    loadRegFile("/resource/disable.reg");
}

public void loadRegFile(String filePath){
    try {
        //create temp file
        File temp = File.createTempFile("temp", ".reg");
        temp.deleteOnExit();

        BufferedWriter out = new BufferedWriter(new FileWriter(temp));

        //load an appropriate registry file
        InputStream in = getClass().getResourceAsStream(filePath);
        //copy to temp file
        IOUtils.copy(in,out); 
        out.close();

        //open using regedit
        Desktop.getDesktop().open(temp);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@DavidAlwis see if my edit helps you. be sure to specify proper location in the source tree and correct name of your registry file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.