Java How to create an executable java application?

  • Thread starter Thread starter Tickitata
  • Start date Start date
  • Tags Tags
    Application Java
AI Thread Summary
A user developed a Java program to calculate multiplicative group tables for Z mod n and is seeking to create an executable JAR file for easy access by a non-technical user. The program compiles and runs correctly from the command line, but the user encountered issues when trying to run the JAR file by double-clicking it, as it produced no output. The discussion provides guidance on creating the JAR file with the correct structure and a manifest file, which includes specifying the main class. It is noted that the program may not display output when run directly from the JAR file due to the absence of a GUI, and running it via the command line with "java -jar ZnX.jar" is suggested as a solution. Additionally, it is mentioned that if using Windows, the Java Virtual Machine (JVM) icon should appear in the system tray when the JAR is executed.
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!
 
Technology 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top