Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to create an executable java application?

  1. Mar 9, 2008 #1
    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 (Text):

    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!
     
  2. jcsd
  3. Mar 9, 2008 #2

    Eus

    User Avatar

    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 (Text):

    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 (Text):

    Manifest-Version: 1.2
    Main-Class: ZnX
    Created-By: 1.4 (Sun Microsystems Inc.)
     
    Good luck!

    Best regards,
    Eus
     
  4. Mar 9, 2008 #3
    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
     
  5. Mar 12, 2008 #4

    Eus

    User Avatar

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

    Code (Text):

    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!

    Best regards,
    Eus
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to create an executable java application?
Loading...