Mendy
Mendy

Reputation: 55

Running a simple Hello World program

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.

Upvotes: 3

Views: 1157

Answers (3)

La bla bla
La bla bla

Reputation: 8708

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)

Upvotes: 1

duffymo
duffymo

Reputation: 308753

You only need a .class file. You don't need a JAR file or an IDE or Maven.

Follow this to the letter. You'll get it to run.

Upvotes: 4

logan
logan

Reputation: 3365

Maven is a very popular tool for managing building and packaging java applications.

For example, set up your POM and use this command to make a JAR file.

mvn package

Once you have a jar file, you can run it using java.

java path/to/MyHelloWorld.jar

Upvotes: 0

Related Questions