How to create an executable java application?

In summary, the conversation discusses creating a small program to calculate multiplicative group tables for Z mod n and converting it into an executable JAR file that can be easily run by a computer illiterate math teacher. The code is written in Java and does not use any GUI or applets. To create the JAR file, a text file named MANIFEST.MF must be created and the JAR command must be used. The JAR file can be run by double-clicking on it, but if it doesn't work, there may be an issue with the JVM settings under Windows.
  • #1
Tickitata
29
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
  • #2
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:
  • #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
 
  • #4
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
 

1. How do I create an executable java application?

In order to create an executable java application, you will need to first write your java code using a text editor or an Integrated Development Environment (IDE) such as Eclipse or NetBeans. Once your code is written, you will need to compile it using the "javac" command in the command line. This will generate a .class file, which contains the compiled java code. Finally, you can use the "java" command to run your application by specifying the name of the class containing the main method.

2. Can I create an executable java application without using an IDE?

Yes, you can create an executable java application using just a text editor and the command line. However, using an IDE can make the process easier and more efficient as it provides features such as code completion, debugging, and project management.

3. How do I make my java application executable on different operating systems?

In order to make your java application executable on different operating systems, you will need to create a JAR (Java Archive) file. This is a compressed file that contains all the necessary files and resources for your application to run. You can create a JAR file using the "jar" command in the command line. This will generate a platform-independent executable file that can be run on any operating system with Java installed.

4. Can I distribute my java application as an executable file?

Yes, you can distribute your java application as an executable file by creating a JAR file. This allows users to run your application without needing to have Java or any other dependencies installed on their system. Additionally, you can also use third-party tools such as Launch4j or JSmooth to create an executable file that can be double-clicked to run your java application.

5. How do I add external libraries to my executable java application?

In order to add external libraries to your executable java application, you will need to specify them in the classpath when compiling and running your code. You can do this by using the "-classpath" or "-cp" option followed by the path to the library or JAR file. Alternatively, you can package the external libraries with your application by including them in the JAR file.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top