1

I have a simple java project in eclipse. The main(String[] args) method of this project takes an argument. Argument to main(String[] args) method is provided inside the 'Program Arguments' text box of 'Arguments' tab of 'Run Configurations' window (Main Class -> Run As -> Run Configurations ->Arguments -> Program Arguments). I want to convert this project into .exe file such that it can read argument from a specific file. On double clicking .exe file, the program should read argument from the specific file and execute the application.

I don't want to hard code the location of file name inside the application because this .exe file will run on many machines, so file path of the this file will change on every machine. Also user is allowed to change the argument inside the file. So I believe that this file should be outside the .exe file but somehow .exe file can read this file.

Is there any way to achieve it? If this is possible, then, how it could be done. Also what should be the extension of this file (.txt or something else).

teobais
  • 2,820
  • 1
  • 24
  • 36
user1432292
  • 57
  • 2
  • 9
  • 1
    Possible duplicate of [How can I convert my Java program to an .exe file?](http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file) – Razib Dec 31 '15 at 10:43
  • This is actually 2 questions, the first being a duplicate (how to crate a Windows executable from a Java program) and the second having several possible good answers. You should edit the question to eliminate the portion that is duplicate, then wait for and accept an answer that addresses the "read arguments from file" portion. – E-Riz Dec 31 '15 at 14:09

1 Answers1

2

Address this as two separate problems:

  1. creation of a ".exe" file, and

  2. implementation of a mechanism for getting the "argument" from a file without hard-coding a file location.

Problem #1 has been addressed many times before; e.g. How can I convert my Java program to an .exe file?

Problem #2 has a number of possible solutions. For example a neat solution would be to create a launch script that passes the argument file path as an extra parameter. Using the Bourne shell, I would write the wrapper like this:

#!/bin/sh
FILE_PATH=<...fill..in..path..here..>
myapp.exe --argfile "${FILE_PATH}" "$@"

(I won't attempt to translate that into a BAT file ... but it should be straight-forward.)

Other solutions would be to put the argument file in a conventional location; (e.g. "%USERPROFILE%\argfile.txt"), or use Java preferences to set the file's location.


Also what should be the extension of this file (.txt or something else).

Java doesn't care what extension you use. However, if you intend for people to be able to edit the file with a Windows text editor, ".txt" would make that easier.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216