0

How would I be able to run both of these classes in one executable file. I'm new to this so sorry if the answer is very simple. I would appreciate it if you were able to walk me through it. Thank you for taking the time to respond.

import java.util.HashSet;

public class Prog2 {
    public static boolean sameElements(int[] A, int[] B){
        boolean same = false;
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for(int i = 0; i < A.length; i++){
            hashSet.add(A[i]);
        }
        for(int i = 0; i < B.length; i++){
            if(hashSet.contains(B[i])){
                hashSet.remove(B[i]);
            }
        }
        if(hashSet.isEmpty()){
            same = true;
        }
        return same;
    }
}

public class Prog3 {
    public static void inRun(int[] A){
        boolean inRun = false;
        for(int i = 0; i < A.length; i++){
            if(inRun) {
                if (A[i] != A[i - 1]) {
                    System.out.print(')');
                    inRun = false;
                }
            }
            else{
                if(i + 1 < A.length && A[i] == A[i + 1]){
                    System.out.print('(');
                    inRun = true;
                }
            }
            System.out.print(" " + A[i] + " ");
        }
        if(inRun){
            System.out.print(')');
        }
    }
}
  • Possible duplicate of [How do I run a Java program from the command line on Windows?](https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows) – Naman Sep 18 '17 at 01:22
  • Do you mean you want to wrap them into a binary executable like a Jar or native Exe? Or you do you mean you want to have a third class which can then run them itself? – MadProgrammer Sep 18 '17 at 01:25
  • please clarify. I can't tell if you just want to add them both in a jar – Alex Jone Sep 18 '17 at 01:25
  • Possible duplicate of [How can I add files to a Jar file?](https://stackoverflow.com/questions/12239764/how-can-i-add-files-to-a-jar-file) – Alex Motley Sep 18 '17 at 01:34
  • So sorry I meant how could I add a third class that could run them both. – Enter Rift Sep 18 '17 at 01:36
  • your last comment is very confusing, please edit your post to clearly state what the question is. – Gilles Gouaillardet Sep 18 '17 at 03:29

1 Answers1

0

Your classes do not have main functions so they cannot be executed. But if they did have main functions:

First, they are both public classes so you need to put each in its own file. (i.e. Prog2.java and Prog3.java) Also, they would be in the same package so you would add package com.domain.progs; to the top of both files.

Second, you would have to call one class from another. So put this in Prog2's main function:

Prog3.main();

Third, you need to put the whole package in a jar file. Your package structure in the source directory should look like this:

com
|____domain
    |_______progs
           |______Prog2.java
           |______Prog3.java

Compile the files so your out (or bin, etc.) looks like that ^ but with class files instead of java files.

Fourth, create a manifest file that specifies which class to execute. Type this into a file called MANIFEST.MF (make sure to not include any trailing white space):

Main-Class: com.domain.progs.Prog2

Lastly, add everything to a jar file. I don't remember the exact syntax to use but I know it starts with this:

jar cvmf ...

Just look up the jar command syntax and make sure to include the manifest file and the com directory that has the class files, not the java ones

That's it. You can now run your jar file. To do that type this command:

java -jar JarFileName.jar

P.S. If you want something you can double click to run, it must be a GUI application; otherwise it won't start a console to show your program in

alexm
  • 51
  • 5