Thursday, January 21, 2016

Error: Could not find or load main class in Java

Following are some of the major causes of this error.

1. Misspell the class name. For example you want to execute command "java HelloWorld", but did "java HaloWorld".

2. Use <class name>.class or <class name>.java. For example use command "java HelloWorld.class" or "java HelloWorld.java" instead of "java HelloWorld".

3. The class path is not set for the class you execute. You may fix this by doing one of the followings.           A. Add class path to the CLASSPATH environment  variable and make sure it is correct.

          B. Use the -cp or -classpath option when execute the class.
                 For example, java -cp C:\Test MyTest

4. The jars required for executing your class are not set in your CLASSPTH,

5. When the class is in a package, it is not executed in the right directory or with the correct complete name.
          For example, the CareMaster class is created in the com.care.gui package.

          A. You must execute it using its complete name,

                java com.care.gui.CareMaster

                if you use "java CareMaster", it will cause the "Could not find or load main class CareMaster" error.

         B. You must execute the class in the directory containing the com directory.
     
6. Execute a jar that does not have a Manifest.mf file or the Main-Class and/or the Class-Path in the manifest.mf are not correctly set.

If none of the above works, try to close all your IDEs and applications, wait for a few minutes and retry. Or turn off your computer, let it rest for a few minutes.

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

                        


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.

References

1. Run .jar file in Windows
2. How to add jars to the class path of your main application when you are doing a distribution/software release



Tuesday, January 19, 2016

Pause execution in Java - sleep() and wait()

You may use the following two methods to pause the execution of the current thread when needs to wait for a required condition.

1. Thread.sleep(<milliseconds>)


       Public class PauseDemo {
             private String[] collectInput() {
                   InputFrame input = new InputFrame();
                   input.run();
                 
                   while (!input.submitted && input.isVisible()) {
                         try {
                                //Make the current thread sleep for one second
                                Thread.sleep(1000);
                         } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                   }

                   String[] result = new String[3];
                   result[0] = input.getName();
                   result[1] = input.getAddress();
                   result[2] = input.getPhone();

                   return result;
             }

            private class InputFrame extends JFrame implements Runnable {
                  boolean submitted = false;

                  JTextField nameField = null;
                  JTextField addressField = null;
                  JTextField phoneField = null;

                  public InputFrame() {
                         JLabel nameLabel = new JLabel("Name: ");
                         JLabel addressLabel = new JLabel("Address: ");
                         JLabel phoneLabel = new JLabel("Phone: ");

                         nameField = new JTextField(9);
                         addressField = new TextField(30);
                         phoneField = new JTextField(10);

                         JButton submit = new JButton("Submit");
                         submit.addMouseListener(new MouseAdapter() {
                               public void mouseClicked(MouseEvent e) {
                                      submitted = true;
                                      InputFrame.this.setVisible(false);
                               }
                         });

                         setLayout(new GridLayout(3, 2);
                         add(nameLabel);
                         add(nameField);
                         add(addressLabel);
                         add(addressField);
                         add(phoneLabel);
                         add(phoneField);
                  }

                  public String getName() {return nameField.getText(); }
                  public String getAddress() {return addressField.getText(); }
                  public String getPhone() {return phoneField.getText(); }

                  public void run() {
                         pack();
                         setSize(800, 600);
                         setLocation(400, 300);
                         setVisible(true);
                  }
           }

       }

2. class wait(<milliseconds>)


In the above case, replacing Thread.sleep(1000) with PauseDemo.class.wait(1000) will also work.

3. TimeUnit

//sleep one second
TimeUnit.SECONDS.sleep(1);

//sleep two minute
TimeUnit.MINUTES.sleep(2);

The good thing about this approach is that it is very easy to know the time it is going to sleep without having to convert from milliseconds.

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

                        
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, January 15, 2016

Create Directory / Directories in Java

You can use the mkdir and mkdirs method of the File class to create a directory or a layer of 
directories.

1. Create a directory

This is the sample code of creating a DATA directory in your current directory.

       import java.io.File;

       public class CreateADirectory {
             public static void main(String[] args) {
                   //Create a File class with the directory name
                   File dir = new File("Data");

                  //Create the directory here
                  boolean suc = dir.mkdir();

                  if (suc) {
                        System.out.println("The Data directory is successfully created.");
                  } else {
                          System.out.println("Failed to create the Data directory");
                  }
            }
       }

2. Create a layer of directories

Here is the sample code of creating a directory and all its parent directories that do not exist.

        import java.io.File;

        public class CreateDirectories {
               public static void main(String[] args) {
                     //Create a File class with the directory name
                     File dir = new File("Company\\Employee\\Data\\PersonalInfo");

                    //Create the directory here
                     boolean suc = dir.mkdirs();

                     if (suc) {
                           System.out.println("All directories are successfully created.");
                     } else {
                            System.out.println("Failed to create directories");
                     }
               }
        }

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

                        
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.