1

I tried searching for an answer to this but nothing I've found has worked. I am trying to execute a java application from a batch file but can't get the application to pickup my input text file directory. I'll illustrate what I mean.

In my java application, I have the following:

String myFile = System.getProperty("myfile");

System.err.println(myFile);

And in my batch application that is running the program I have:

set -Dmyfile="C:\directory"

However, when I actually run the program, it seems that myfile isn't being picked up as a system property because null is printed as the value of myFile. Could anyone offer some help please?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • what does the `-D` portion do in your `set` command line?? remove it and try again... – aschipfl Aug 06 '15 at 15:18
  • i dont think you need the `set` it would be `java -jar -Dmyfile="C:\directory" jarname.jar` – j.con Aug 06 '15 at 15:24
  • Thanks, I added the -D because several sites I looked at indicated that in order to set a variable as a system property, the -D was necessary. Unfortunately, removing it didn't fix my problem. –  Aug 06 '15 at 15:26
  • Hi J. Con, thanks for the response, if I were to use the syntax you indicated might work, would I need to store the text file in a jar? –  Aug 06 '15 at 15:31
  • You just asked how to get a system property via code which was set by command line arguments. Are you trying to read the actual text file? because that is a lot different – j.con Aug 06 '15 at 15:38
  • What does your batch file look like? Perhaps you are missing the parameter passing into it. If your batch looks like `java -jar jarname.jar` you should add `java %1 -jar jarname.jar` to echo the first command-line arg given to the batch file within what the batch file executes. – Paul Bilnoski Aug 06 '15 at 15:43
  • Hi J.Con, please let me know if this clarifies it. In my java code, I read a text file whose directory I store in myFile. The only thing I am trying to pass to the java code is the directory of this file via the batch. Alternatively I could just set String myFile = "C:\\directory" but I want to have single click execution of my program via a batch file. –  Aug 06 '15 at 15:47
  • take a look at http://stackoverflow.com/questions/2271926/how-to-read-a-file-from-a-jar-file hopefully that will point you in the right direction – j.con Aug 06 '15 at 15:57
  • To set a system property through a batch file you have to do: `set myfile = "c:\directory"` the -D is a parameter to set variables to the jvm. – Jorge Campos Oct 07 '15 at 00:31

0 Answers0