2

I want to run a program using the following commands

java Main -host arg1 arg2

However I have faced some troble as I had made the project in separate packages. So I get a jar file and cannot run the main from the above keywords. I have to call the jar file thus from the command line.

java -jar Main.jar arg1 arg2

However I do not want that as it goes against my specification.

Can you help me please?

SPECIFICATION

A text-only application is all that is required. It must be networked and run as either a host (server) or a client from the command line using the commands:

java BankApp -host PORT
java BankApp -client MACHINE PORT

to start as host or client respectively. For example, if you know the host is running on aloha at port 8888 you would connect with:

java BankApp -client aloha 8888

what I need to do to run is as follows.

java -jar C:\Users\UniversityofBristol\Desktop\BankApp\BankApp\dist BankApp.jar arg1 arg2

public class BankApp {

public static void main(String[] args) throws IOException, ClassNotFoundException {


    String current = new java.io.File(".").getCanonicalPath();
    current = "java -jar " + current + "\\dist";
    current = "\"" + current + "\"";
    current = current + " BankApp.jar";
    System.out.println("Current dir:" + current);

    // Run a java app in a separate system process
    Process proc = Runtime.getRuntime().exec(current);
    // Then retreive the process output
    InputStream in = proc.getInputStream();
    InputStream err = proc.getErrorStream();

}

}

2 Answers2

1

See this answer if you want to run the .jar in a separate process.

Otherwise I think you can just add it to your project (look under project properties in whatever IDE you are using) and use it as any other library.

Community
  • 1
  • 1
Marconius
  • 683
  • 1
  • 5
  • 13
0

Just put your jar on the classpath. You could break the spec a little bit, by saying:

java -cp bankapp.jar BankApp arg1 arg2

or set the $CLASSPATH environment variable (which is ugly and I never really do it outside scripts but it works):

export CLASSPATH=path/to/your/jar
java BankApp arg1 arg2

Spoiler

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268