Search code examples
javajarpackageembedded-resourceexecutable-jar

Package java class


I built a small program that displays the contents of a text file in the src folder of the package, in this case, it displays pi to a million decimal places. (code)

import java.io.*;
import java.util.Scanner;

class piMillion
{
    public void method() throws IOException
    {
        Scanner fileIn = new Scanner(new File("src/pi.txt"));
        boolean rem = fileIn.hasNext();
        while (rem==true)
        {
            String line = fileIn.nextLine();
            System.out.println (line);
        }
    }
}

How can I package the program so that it runs independently, and displays the output in a new window?


Solution

    1. The class and text file needs to be put in a runnable Jar. For details, see:
      1. Packaging Programs in JAR Files
      2. Setting an Application's Entry Point.
    2. To fulfill the 'entry point' requirements it will need a method with signature:

      public static void main(String[] args)
      
    3. This line needs to change from File to URL in order to work for an .

      Scanner fileIn = new Scanner(new File("src/pi.txt"));
      

      See the info. page for details on how to form the URL.

    4. System.out.println (line); The output of System.out is not seen for a runnable Jar. It will need a GUI. See Creating a GUI With JFC/Swing for further details.