IT-info > Java

Java

Java programming (Code reference & info)

https://www.thoughtco.com/create-a-simple-window-using-jframe-2034069
beginwithjava.blogspot.se/2008/07/java-graphics-start-with-jframe.html

http://stackoverflow.com/questions/2011664/compiling-a-java-program-into-an-executable
https://www.excelsior-usa.com/articles/java-to-exe.html
http://www.javalobby.org/articles/java2exe/
Java .jar to Windows .exe Tutorial (YouTube)
Make Java Executable (YouTube)
Compiling Java applications to native Windows executables
https://www.thisiscool.com/gcc_mingw.htm

http://stackoverflow.com/questions/19650711/how-to-create-a-java-jar-file-with-source-code-java-files
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

How to create and run a .jar file

http://stackoverflow.com/questions/1238145/how-to-run-a-jar-file

Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:

HelloWorld.java:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello world!");
    }
}

manifest.txt:

Manifest-version: 1.0
Main-Class: HelloWorld

Note that the manifest.txt text file must end with a new, empty line. The last line will not be parsed properly if the file does not end with a new, empty line.

After saving the files HelloWorld.java and manifest.txt respectively, in Windows command prompt, a command console etc. , move into the folder/location where the files are saved and run the following commands:

javac HelloWorld.java
jar -cmf manifest.txt helloworld.jar HelloWorld.class
java -jar helloworld.jar

Output:

Hello world!

jar command options:

The c option indicates that you want to create a JAR file.
The f option indicates that you want the output to go to a file rather than to stdout.

You can add any of these additional options to the cf options of the basic command:
v   Produces verbose output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.
0 (zero)   Indicates that you don't want the JAR file to be compressed.
M   Indicates that the default manifest file should not be produced.
m   Used to include manifest information from an existing manifest file. The format for using this option is: jar cmf existing-manifest jar-file input-file(s)
-C   To change directories during execution of the command. See Oracle documentation Creating a JAR File for an example.

Create a simple GUI window

https://www.thoughtco.com/create-a-simple-window-using-jframe-2034069

import java.awt.*;
import javax.swing.*;

// Create a simple GUI window
public class TopLevelWindow {
    private static void createWindow() {

        //Create and set up the window.
        JFrame frame = new JFrame("Simple GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(300, 100));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);

        //Display the window.
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createWindow();
    }
}