Tuesday, March 31, 2015

Cursor automatically moves / jumps around when typing

When a wireless mouse is used, the cursor sometimes automatically moves to unpredictable places when typing. To fix this problem in a Windows 8 DELL computer, follow the steps below.

1. Adjust the mouse settings.

  • Open the control panel, click on "Hardware and Sound", then under "Devices and Printers" click the "Mouse" hyperlink. 
  • In the pop-up Mouse Properties window, click the link "Click to change the Dell Touchpad settings". 
  • In the pop-up window, click the mouse icon and check the "Disable Touchpad when USB Mouse present" check box. Close the window
  • Click the "Apply" button and the "OK" button.
      If step 1 does not completely fix the problem, continue to do step 2.

2. Adjust the keyboard  setting.

  • Open the control panel, click on the "Easy of Access", then under the "Easy of Access Center" click the "Change how your keyboard works" link.
  • At the bottom of the pop-up window, click the "Keyboard Settings" link.
  • In the pop-up Keyboard Properties window, select the "Speed" tab, increase the repeat delay and decrease the repeat rate by sliding the indicators to the left.
  • Click the "Apply" button and the "OK" button.
      If after step 2, the problem still exists, continue to do step 3.

3. Install the TouchFreeze. TouchFreeze can be downloaded at TouchFreeze.

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

                        
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.

Tuesday, March 10, 2015

com.jcraft.jsch.JSchException: Packet corrupt

This exception occurs when the Session is reused repeatedly in a loop where the session is disconnected intentionally or due to time out and needs to reconnect again.

For example, the following code will throw such an exception at session.connect() in the while loop.

Public class SftpTest extends Thread {
       private String username = "";
       private String password = "";
       private boolean running = true;

      public SftpTest(String user, String pass) {
            username = user;
            password = pass;
      }

      public void run() {
             try {
                    JSch jsch = new JSch();
                     Session session = jsch.getSession(username, "<your host>");
                    session.setPassword(password);
                    session.setConfig("StrictHostKeyChecking", "no");
                   session.connect();

                   Channel channel = session.openChannel("sftp");
                   channel.connect();
                    ChannelSftp sftp = (ChannelSftp) channel;

                    while (running) {
                           if (!session.isConnected()) {
                                   session.connect();
                                   channel = session.openChannel("sftp");
                             }
                            if (!channel.isConnected()) {
                                   channel.connect();
                                  sftp = (ChannelSftp) channel;
                           }

                           Vector<LsEntry> entries = sftp.ls("*.pdf");
                           for (LsEntry entry : entries) {
                                   String fileName = entry.getFilename();
                                   sftp.get(fileName, ".");
                                   sftp.rm(fileName);
                            }
                            Thread.sleep(60 * 60 * 1000);//sleep for 1 hr                        
                      }
                       sftp.exit();
                       sftp.disconnect();
                       session.disconnect();
              } catch (InterruptedException ie) {
                     ie.printStackTrace();
              } catch (SftpException fe) {
                     fe.printStackTrace();
              } catch (JSchException e) {
                    e.printStackTrace();
              }
       }

       public static void main (String[] args) {
               SftpTest test = new SftpTest("<your username>", "<your password>");
               test.start()
       }
}

The reason that such an exception is thrown is that the first time the Session is connected to the remote site, a random number called Packet is generated for the session. When the thread is having its 1 hour sleep, the session gets automatically disconnected due to no activity for a certain period of time. When the Session is disconnected, the Packet is lost. When the Session is trying to reconnect, it could not find the Packet, thus the exception is thrown.

One way to solve this problem is to not reuse the same Session. If the thread is going to sleep for awhile, disconnect the Session before going to sleep and create a new Session after sleep. The above code can be rearranged as below to avoid this exception.

Public class SftpTest extends Thread {
       private String username = "";
       private String password = "";
       private boolean running = true;

      public SftpTest(String user, String pass) {
            username = user;
            password = pass;
      }

      public void run() {
             try {
                   while (running) {
                         JSch jsch = new JSch();
                         Session session = jsch.getSession(username, "<your host>");
                         session.setPassword(password);
                         session.setConfig("StrictHostKeyChecking", "no");
                         session.connect();

                        Channel channel = session.openChannel("sftp");
                        channel.connect();
                        ChannelSftp sftp = (ChannelSftp) channel;

                        Vector<LsEntry> entries = sftp.ls("*.pdf");
                        for (LsEntry entry : entries) {
                                   String fileName = entry.getFilename();
                                   sftp.get(fileName, ".");
                                   sftp.rm(fileName);
                         }
                       sftp.exit();
                       sftp.disconnect();
                       session.disconnect();
                      Thread.sleep(60 * 60 * 1000);//sleep for 1 hr
                   } //end of while loop
             } catch (InterruptedException ie) {
                     ie.printStackTrace();
              } catch (SftpException fe) {
                     fe.printStackTrace();
              } catch (JSchException e) {
                    e.printStackTrace();
              }
       }

       public static void main (String[] args) {
               SftpTest test = new SftpTest("<your username>", "<your password>");
               test.start()
       }
}
       
----------------------------------------------------------------------------------------------------------------
                        
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.

Friday, March 6, 2015

DynamicReports: How to save a report for viewing later

Step 1. Create and Save the report

public class DynamicReportsTest {
       public DynamicReportsTest() {
            JasperReportBuilder report = DynamicReports.report();

            TextColumnBuilder<Float> priceColumn = Columns.column("Price", "price", DataTypes.floatType());
             TextColumnBuilder<Integer> quantityOrderedColumn = Columns.column("Quantity Ordered", "quantityOrdered", DataTypes.integerType());
             TextColumnBuilder<BigDecimal> totalPay = priceColumn.multiply(quantityOrderedColumn)
                .setTitle("Amount Paid"));

            report.columns(Columns.columnRowNumberColumn("Item"),
                Columns.column("Name", "name", DataTypes.stringType()),
                priceColumn,
                quantityOrderedColumn,
                totalPay);
             report.title(Components.text("Test Report").setStyle(stl.style().blod());

            report.setDataSource(createDataSource());
            try {
                   //Save the report to a file.
                  JRSaver.saveObject(report.toJasperPrint(),  "<file path and name>");
                   report.show();
            }
      }

       private JRDataSource createDataSource() {
              DRDataSource dataSource = new DRDataSource("name", "price", "quantityOrdered");
              dataSource.add("Apple", new BigDecimal(1.29), 120);
              dataSource.add("Apple", new BigDecimal(1.69), 150);
              dataSource.add("Orange", new BigDecimal(0.99), 130);
              dataSource.add("Orange", new BigDecimal(0.96), 100);
             dataSource.add("Mange", new BigDecimal(0), 300);
             return dataSource;
      }
 
      public static void main(String[] args){
           new DynamicReportsTest();
      }
}


Step 2. View the saved report

public class viewSavedReport {
      public static void main(String[] args) {
              //View the saved report in step 1
             JasperPrint rpt = (JasperPrint)JRLoader.loadObject(new java.io.FileInputStream("<file path and name>"));
              reportViewer = new JasperViewer(rpt, false);
             reportViewer.setTitle("<Your report viewer title>");
             reportViewer.setZoomRatio(new Float(0.8949)); //the frame fit ratio
             reportViewer.setVisible(true);
      }
}
       
-----------------------------------------------------------------------------------------------------------------------
                        
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.