How to create an executable java application?

  • Context: Java 
  • Thread starter Thread starter Tickitata
  • Start date Start date
  • Tags Tags
    Application Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 12K views
Tickitata
Messages
29
Reaction score
0
Hi, I've recently written a small program to calculate multiplicative group tables for Z mod n. I wrote it in java, as that's the only programming language I currently know.

Code:
import java.util.*;
        
public class ZnX 
{
        
    public static int gcd(int a, int b) 
    {
 
        if (b==0) 
            return a;
        else
            return gcd(b, a % b);
    } 
    
    public static void main(String[] args)
    {
        final int TWO_DIGITS = 10;
        
        
        int n = 0;
        int[] primes = new int[0];
        
        System.out.println("Multiplication table for Z modulo n");
        System.out.println("Please enter n: ");
        
        Scanner scan = new Scanner(System.in);
        
        // Calculating Relatively Prime Integers
        while(scan.hasNextInt()) 
        {
            if(scan.hasNextInt())
            {
                n = scan.nextInt();
                primes = new int[n];
    //           System.out.print("The set of relatively prime integers to " +  n + " is: {");
            }
            else
            {
                System.out.println("Input is not an integer, terminating program");
                System.exit(0);
            }

            int j = 0;        
            for(int i = 0; i <= n; i++) 
            {            
                if(gcd(i,n) == 1) 
                {
                    primes[j] = i;
    //                System.out.print(i + ", ");
                    j++;
                }
            }    

    //        System.out.println("}");
    //        System.out.println("Number of elements: " + j);

            System.out.println("The multiplication table for Z modulo " + n + " is:");

            int[][] multTable = new int[j][j];
            for(int row = 0; row < j; row++) 
            {
                for(int col = 0; col < j; col++) 
                {
                    multTable[row][col] = (primes[row] * primes[col]) % n;
                    System.out.print(multTable[row][col]);

                    if((multTable[row][col]) < TWO_DIGITS)
                        System.out.print("  ");
                    else
                        System.out.print(" ");                 
                }
                System.out.print("\n");

            } // End for loop  
            
            System.out.println("\nPlease enter another integer: ");
        }  // End while loop
        
        System.out.println("End of valid integers, terminating program");

    } // End main
} // End class

This code compiles and runs correctly from the commandline. Now, because I'm writing this for my computer illiterate math teacher, I'd like to create an executable file (.jar?) which she can simply double click to run

Is there any way to do this without using GUI/applets?

Thanks!
 
Physics news on Phys.org
Hi Ho!

Sure, your teacher can run your Java program just by double-clicking the JAR file.

The complete information is here:
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/manifest.html
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html

Basically, just create a jar with the following structure:
+-- META-INF/ -- MANIFEST.MF
|
+-- ZnX.class
with the following command:
Code:
jar cvfm ZnX.jar MANIFEST.MF ZnX.class

Notice that you have to create a text file named MANIFEST.MF prior to running the command above. The file should contain the following lines:
Code:
Manifest-Version: 1.2
Main-Class: ZnX
Created-By: 1.4 (Sun Microsystems Inc.)

Good luck!


Eus
 
Last edited by a moderator:
Thanks for the tips, Gus!

Could you try it out using my code? When I double click the jar file, it doesn't seem to do anything. No errors, but no output either

I was hoping that the program would run in the terminal like it normally does. Is this because I don't use any GUI?

Thanks for your help
 
Tickitata said:
Thanks for the tips, Gus!

Could you try it out using my code? When I double click the jar file, it doesn't seem to do anything. No errors, but no output either

I was hoping that the program would run in the terminal like it normally does. Is this because I don't use any GUI?

Thanks for your help

I had tried it and I could run the JAR file nicely in the command line with the following command:

Code:
java -jar ZnX.jar

If you try to run the JAR file by double-clicking on the file and you use Windows, the icon of the JVM should appear in the tray icon.
Otherwise, there might be something wrong with the setting of your JVM under Windows.

Good luck!


Eus