How to create an executable java application?

  • Context: Java 
  • Thread starter Thread starter Tickitata
  • Start date Start date
  • Tags Tags
    Application Java
Click For Summary

Discussion Overview

The discussion revolves around creating an executable Java application, specifically a JAR file, from a Java program that calculates multiplicative group tables for Z mod n. The focus is on how to enable a user, particularly a non-technical one, to run the application easily without a graphical user interface.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant shares their Java code and expresses the desire to create a JAR file that can be double-clicked to run.
  • Another participant explains how to create a JAR file, detailing the necessary structure and contents of the MANIFEST.MF file.
  • A participant reports issues with the JAR file not producing output when double-clicked, questioning whether the lack of a GUI is the cause.
  • Another response suggests that running the JAR file from the command line works, indicating that the issue may be related to the user's Java Virtual Machine (JVM) settings on Windows.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the issue of the JAR file's execution behavior when double-clicked, as some suggest it should work while others report it does not. The discussion remains unresolved regarding the specific cause of the output issue.

Contextual Notes

There are indications of potential limitations related to the user's JVM settings and the execution environment, but these are not fully explored or resolved in the discussion.

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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
8K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K