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?
To fulfill the 'entry point' requirements it will need a method with signature:
public static void main(String[] args)
This line needs to change from File
to URL
in order to work for an embedded-resource.
Scanner fileIn = new Scanner(new File("src/pi.txt"));
See the info. page for details on how to form the URL.
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.