Run a simple Hello World program

advertisements

I made a simple hello world program which just makes a window pop up with the title being "hello world". I want to know, What do I give to someone if I want them to run it like a normal java program? Would a .class file be enough?

do I just take the .class file and try to open it with java?

Here is the code:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class hellobox {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello world!");
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

I'm so sorry for such a pathetic question. first time fiddling with java.


If you're using eclipse, you could do this:

  1. Right click your project
  2. Click Export
  3. Select Java
  4. Select Runnable Jar File
  5. Select the Launch Configuration (would usually be the name of your project or class that contains the main method
  6. Choose the path to save the jar file to
  7. Click Finish

That's it. Now you get a .jar file which can be run like a normal executable file on every computer which has Java installed. Regardless of the operation system.

Just passing the .class file would suffice, but it would force the other user to launch it via command line using java MyClassName (without the .class suffix)