0

What are some ways to interact with a jar post creation in the command line? I know you can pass parameters when it's created (e.g. java -jar foo.jar foo bar) but this isn't what I want.

Example of what I would like to happen

java -jar foo.jar

Some time later

// Someway to interact with the above jar e.g. Call a method
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
GregWringle
  • 445
  • 3
  • 6
  • 16
  • What type of interaction are you referring to? – Prasanna Dec 01 '15 at 17:17
  • Any way you could possibly image. And more. – Boris the Spider Dec 01 '15 at 17:20
  • Possible duplicate of [How to call method in jar file with terminal?](http://stackoverflow.com/questions/7950538/how-to-call-method-in-jar-file-with-terminal) – azurefrog Dec 01 '15 at 17:20
  • I would like to call a method and pass parameters to the method via command line. – GregWringle Dec 01 '15 at 17:21
  • @GregWringle why? Why focus on methods? This exposes far too much of the internals of the application to the end user. Look into, for example, JMX. Or you could go the whole way and use an HTTP server and REST (for another example). – Boris the Spider Dec 01 '15 at 17:21
  • It's the first thing that came to mind. I guess what I really want is to pass a jar a set of parameters and have it do something with them after the jar has been started. – GregWringle Dec 01 '15 at 17:23
  • Who not do _exactly that_ `java -jar myJar.jar --do-some-magic-stuff` for example? – Boris the Spider Dec 01 '15 at 17:28
  • That does it once and I would like to pass different parameters at a later time and maybe multiple times. I'm getting the feeling this is difficult for Java and I might be better off with C++. – GregWringle Dec 01 '15 at 17:36
  • you want to execute multiple times ? (add a parameter for that), or you want to give input after it starts ? (then use console and System.in) – guillaume girod-vitouchkina Dec 01 '15 at 17:38
  • I want to give input after it starts and have it do something with that input. Correct me if I'm wrong but System.in only works for the terminal that it's in correct? – GregWringle Dec 01 '15 at 17:43
  • Possible duplicate of [What is "String args\[\]"? parameter in main method Java](http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) – sobolevn Dec 01 '15 at 17:43
  • All the uses with public static void main(String args[]) args is once the jar is created, no? – GregWringle Dec 01 '15 at 17:48
  • It is no different in Java than in C++ or any other language. You first need to define what exactly the interaction is, then you need to implement it. – Boris the Spider Dec 01 '15 at 17:54
  • Ok what I want is to create a system where there is a central processor that works on tasks. Tasks would be sent to the central processor at random, in both amount and time sent. e.g. - Central Processor start - Task 0 comes at time 0 - Task 1 comes at time 1 - Task 2 comes at time 6 - Task 3 comes at time 6 - Task 4 comes at time 10 – GregWringle Dec 01 '15 at 17:56
  • The user decides when to launch tasks ? then you can use my code (and testing what is on input), or is it configured somewhere ? then you need a scheduler : there exists libraries for that. – guillaume girod-vitouchkina Dec 01 '15 at 18:03
  • Yes the user will decide when to launch a task for the processor to work on. The flow would be something like this. Processor starts up. A task is received and processor works on the task. If any task is received when the processor is busy, add it to a queue. For your code would it only work for the terminal that the jar was created in? – GregWringle Dec 01 '15 at 18:06
  • the console (System.in) is where you start the jar. You definitively need to do some parallel tasks, then you have to use threads at minimum (or other processes, ...) the main process read from console, and launch others tasks. – guillaume girod-vitouchkina Dec 01 '15 at 18:19
  • @GregWringle none of that really answers the big questions. 1) **what** is a task and 2) **how** is a task sent. – Boris the Spider Dec 01 '15 at 18:20
  • A task would be to run through automated tasks, taking in file location, device name, device type. Ideally a task would be sent through command line to the jar. (After the jar has been started) Note: Ideally the Java program would be a command line program. – GregWringle Dec 01 '15 at 18:27
  • Then you should reformulate your question or post a new one, and close this one: "What are some ways to interact with a jar post creation in the command line?" – guillaume girod-vitouchkina Dec 01 '15 at 18:33

2 Answers2

0

A typical method to interact with a jar file is to specify the main class to be called, and then read the arguments from command line.

public static void main(final String[] args) {
   // evaluate command line arguments here
   System.out.println("number of arguments=" + args.length);
   System.out.println("arg1" + args[0]);
}

On command line:

java.exe -cp myjar.jar com.mycompany.Myclass  arg1 arg2 arg3

where MyClass contains the main method.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

see: What is "String args[]"? parameter in main method Java

 public static void main(String args[])
{
int n=args.length; // number of arguments

String arg1=args[0]; // param 1
// then param2, ...

}

Interacting during the program:

see this: How to use readline() method in Java?

    BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
    String line=buffer.readLine();
    System.out.println("LINE:"+line);

If you want to run some task, and in the same time, get some user input, you have to launche yours tasks with a thread: see this for example: Java - creating a new thread

Community
  • 1
  • 1