This discussion is archived
12 Replies Latest reply: Dec 21, 2012 10:36 PM by EJP RSS

Input to an executable jar from a batch file

Raja Pandian C Newbie
Currently Being Moderated
Hi, I have created an executable jar program to receive the input during runtime from a batch file. I have tried to run the program by passing arguments at runtime but the code doesn't seem to execute. Can anyone help me out to pass an argument to the java program using the batch file. The flow of my project is -> The batch file triggers the executable jar file (I am able to execute till this !) -> The input to the executable jar file should be fetched from the arguments given in the batch file which will be taken as the input for the java program.
  • 1. Re: Input to an executable jar from a batch file
    EJP Guru
    Currently Being Moderated
    What have you tried?
  • 2. Re: Input to an executable jar from a batch file
    DrClap Expert
    Currently Being Moderated
    I don't understand what it means to "fetch input from an argument given in a batch file". Perhaps you could provide an example which would illuminate that.
  • 3. Re: Input to an executable jar from a batch file
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    Raja Pandian C wrote:
    The input to the executable jar file should be fetched from the arguments given in the batch file which will be taken as the input for the java program.
    Depends what you mean, so the following is one possibility (should be correct but I didn't verify details.)
    @echo off
    REM Run app
    
    set ARG1=3
    set ARG2=some text
    
    set MY_CLASSPATH=...
    
    java -cp "%MY_CLASSPATH%" com.company.product.MyApp "%ARG1%" "%ARG2%"
    
    echo App is done
    pause
  • 4. Re: Input to an executable jar from a batch file
    Raja Pandian C Newbie
    Currently Being Moderated
    The executable jar should be configured to get an input at runtime which in my case is #filedirectory.

    public static void main(String args[]) throws Exception {

         try
         {


    String appRootDirectory = *"C:\\Users\\Administrator\\Desktop\\JavaEmbedding"*;

    My batch code : java -Xms512M -Xmx512M -jar Program.jar -o true
    PAUSE


    I should replace the input to be fetched at runtime using a batch file. What are the changes to be made in this code to do so ?

    Edited by: Raja Pandian C on Dec 21, 2012 10:38 AM
  • 5. Re: Input to an executable jar from a batch file
    EJP Guru
    Currently Being Moderated
    That's what the String[] args is for.
  • 6. Re: Input to an executable jar from a batch file
    Raja Pandian C Newbie
    Currently Being Moderated
    ya but how to give the input via a batch file. whats the syntax to execute the program with any input ?
    what if i want the user to give the input at runtime. what are the changes to be made in the code ?

    Edited by: Raja Pandian C on Dec 21, 2012 12:35 PM
  • 7. Re: Input to an executable jar from a batch file
    EJP Guru
    Currently Being Moderated
    ya but how to give the input via a batch file.
    The same way you would give it on the command line.
    whats the syntax to execute the program with any input ?
    Ditto. Provide the filename as an argument as shown in answers above.
    what if i want the user to give the input at runtime. what are the changes to be made in the code ?
    I've already answered that. Use the String[] args. This is very basic Java that you should already know.
  • 8. Re: Input to an executable jar from a batch file
    Raja Pandian C Newbie
    Currently Being Moderated
    My java code prompts the user to enter the data but the batch file when executed executes the code but does not wait for the user input. This is the message my batch file displays when executed.

    C:\Users\Administrator\Downloads\BatchTool>java -Xms512M -Xmx512M -jar Program.jar -o true
    Enter custom directory :

    C:\Users\Administrator\Downloads\BatchTool>PAUSE
    Press any key to continue . . .

    If I press any key the program exits

    Is there an option to give the input via batch code ? My doubt is not in the java code. Its the batch file which triggers the code and i want the batch command which will trigger the input to the code being executed.

    Is there a way to give the input to this batch file ?

    C:\Users\Administrator\Downloads\BatchTool>java -Xms512M -Xmx512M -jar Program.jar -o true * Can i replace this code with an input for the java code ? *

    Edited by: Raja Pandian C on Dec 21, 2012 2:26 PM

    Edited by: Raja Pandian C on Dec 21, 2012 2:27 PM
  • 9. Re: Input to an executable jar from a batch file
    abillconsl Explorer
    Currently Being Moderated
    Don't prompt the user for an entry. Instead, as mentioned, use the String array that gets passed into the main method to get the arguments without any prompt, as was shown in above answers. In the java code use this String array something like this:
    previously_prompted_for_parameter = args[0];
  • 10. Re: Input to an executable jar from a batch file
    Raja Pandian C Newbie
    Currently Being Moderated
    the code with the directory pre-defined is an example. I want the user to enter the filedirectory during runtime. i tried with the args[0] but its not working !

    String appRootDirectory = args[0]; (If this is the change you mentioned)

    This code works if i test in an IDE but i want the input to be given via a batch file. What is the batch command to replace args[0] ?

    Can anyone help me out in the batch code. i want the batch code to get the input directory from the user and replace it with args[0].

    My batch code : java -Xms512M -Xmx512M -jar Program.jar -o true
    PAUSE

    Once i run this batch file, it shud ask the user to enter the file directory. The batch command above executes the code but does not wait for the user input !

    * Help me with the batch command. The java code is working fine *
  • 11. Re: Input to an executable jar from a batch file
    Raja Pandian C Newbie
    Currently Being Moderated
    What does "%MY_CLASSPATH%" refer to ?
  • 12. Re: Input to an executable jar from a batch file
    EJP Guru
    Currently Being Moderated
    What is the batch command to replace args[0] ?
    There is no batch command to replace args[0]. The question doesn't even make sense. All you have to do is supply the command line argument in the batch file, and you've already been shown how to do that.
    Can anyone help me out in the batch code.
    You've been spoonfed the batch code already. In fact you've been spoonfed far too much already. What I am missing here is any attempt on your part to actually understand what you've been told here.

    In any case batch code is off topic for this forum. Locking this futile thread.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points