Showing posts with label Execution. Show all posts
Showing posts with label Execution. Show all posts

Tuesday, August 26, 2014

Run .jar file in Windows

1. Specify the Application entry point
      Method 1
            A. Create the manifest.mf file with the following content. This file must ends with an empty line after the last line, otherwise, it will not be processed properly.


           Main-Class: <package>.<file>.class

            B. Create the jar file and specify the application entry point by executing one of the following commands.

                      jar -cfm <file>.jar manifest.mf  <package>/*.class

      Method 2
            Set the entry point to <package>.<file>.class by running the following command.

                   jar -cfe <file>.jar <package>.<file> <package>/<file>.class

2. Put all the jar files and other required files such as properties files if they are not included in the jar already in the same directory.

   To run from the command line, type: java -jar <file>.jar


  To run by double click the jar file, the .jar file type has to be associated with the java execution program. To set the association, do the following.

     A. Open the Control Panel
     B. Go to Programs\Default Programs
     C. Click Associate a file type or protocol with a program
     D. Scroll down the list, double click on the .jar entry
     E. Click the Browse and choose C:\Program Files\Java\jre7\bin\javaw or wherever your java is installed
     F. Click Open and then OK

3. Follow the steps here to create a desktop icon to launch the application by double clicking on the icon.
          A. Right click on desktop, choose New, then Shortcut.
          B. Enter java -jar <file>.jar in the text box, and click Next
          C. Enter a <name> for your shortcut, then click Finish
          D. Right click on the <name> shortcut on your desktop and choose Properties
          E. In the Start in: box, enter the path of the directory where the <file>.jar is located. click Apply.
          F. Click the Change Icon button and browse to select an image to use for your shortcut. Click OK and OK.

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

                        

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.

Monday, January 6, 2014

Run .jar file of a SWing GUI application on a different computer through PuTTY


  1. Check if Xming Server is running on your computer by either looking at the task bar or by checking Xming.exe on the Processes tab of the Windows Task Manager. To open the Windows Task Manager, press Ctrl+Alt+Delete.
  2. Open PuTTY, choose Connection, SSH, then X11.
  3. Check Enable X11 Forwarding. In the X display location, enter localhost:0. For Remote X11 authentication protocol, select MIT-Magic_Cookie-1.
  4. Click on Session, enter the IP address of the remote computer in the Host Name box. If you would like to save your session setting, enter a session name in the Saved Sessions box and click the Save button. 
  5. Click the Open button to start the connection. After logged onto the remote computer, you can check the display setting by typing "echo $DISPLAY". 
  6. Type "jar -jar <file>.jar" to start your application.
---------------------------------------------------------------------------------------------------------------

                        
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, December 27, 2013

Use reflection to dynamically invoke methods

This sample code uses class reflection associated with an ActionListener to dynamically invoke the specified method at runtime. The method name and parameters can be set differently under different conditions and for different source objects.

In the code, class MethodRelection has three JTextField fields and an objModified method. When any of the JTextField fields is modified, the objModified method will be called to handle the modification accordingly.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MethodReflection extends JFrame{
    public MethodReflection() {
        JTextField[] data = {new JTextField("Peach", 20),
                                  new JTextField("Papaya", 20),
                                  new JTextField("Mango", 20)};
        JPanel panel = new JPanel(new GridLayout(3, 1));
     
        for (int i=0; i<data.length; i++){
            try {
                data[i].addActionListener(new ObjectActionListener(this,
                                                   "objModified",
                                                   new Object[]{data[i], "Field "+(i+1)+" Modified"}
                ));
                panel.add(data[i]);
            } catch (NoSuchMethodException e){
                System.out.println(e.getMessage());
            }
        }
     
        this.add(panel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700, 300);
        this.pack();
    }
 
    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                (new MethodReflection()).setVisible(true);
            }
        });
     
     
    }
 
    public class ObjectActionListener implements ActionListener {
        private Object methodSource = null;
        private Object[] parameters = null;
        private String methodName = null;
        private Method method = null;

        public ObjectActionListener(Object methodSource, String methodName, Object[] parameters)
                throws NoSuchMethodException, SecurityException {
            this.methodSource = methodSource;
            this.methodName = methodName;
            if (parameters == null) {
                parameters = new Object[0];
            }
            this.parameters = parameters;
            method = getMethod();
        }

        private Method getMethod() throws NoSuchMethodException {
            Class theClass = methodSource.getClass();

            Class[] paramTypes = new Class[parameters.length];
            for (int i = 0; i < parameters.length; i++) {
                paramTypes[i] = parameters[i].getClass();
            }
            return theClass.getMethod(methodName, paramTypes);
        }

        public void actionPerformed(ActionEvent event) {
            try {
                method.invoke(methodSource, parameters);
            } catch (IllegalAccessException e) {
                System.out.println(e.getMessage());
            } catch (InvocationTargetException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
 
    public void objModified(JTextField obj, String message){
            String text = obj.getText();
            System.out.println(message+": "+text);
    }
}

The code using MethodHandle to dynamically invoke method can be found here

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

                        
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.