Include file in Java project

0

I have a code that runs routines inside Linux servers and I access using public keys. When the project is in my pc (development), everything works because in the code I specify the location of the key that is in my machine, but when generating a jar (package for store) and running on another computer, the program does not find this key .

I have already put the key inside the src / myProject folder and I made reference to the file and locally it worked, but in another machine it did not.

I researched getresources but still could not.

I refer to the file as any string:

String privateKey = "Diretorio\id_dsa.ppk";

What would be the correct way to attach this key to my project so that it runs on any machine?

    
asked by anonymous 19.07.2017 / 20:37

1 answer

1

You are referring to the directory via hard coded, this is not a good idea, in different environments this may not work, for example, Windows uses the infamous "backslash" to separate directories, since linux uses " normal bar ".

Yaml

Good practice in this case is the use of a configuration file, my preference is yaml , some languages, such as python, have packages for yaml parse, I do not know if it has java but has this tutorial .

Example of a yaml configuration file:

linux:
  privateKey: /home/user/keys/id_dsa.ppk  
  ...

windows:
  privateKey: c:\path\keys\id_dsa.ppk  

Then in your app just identify the environment and read the privatekey variable from the correct key (linux or windows).

Environment Variable:

Another option within good practice is the use of environment variables. Your app would have to read an environment variable that points to the file path, example of creating variables:

Creating environment variable in linux:

export PKEY=/home/user/keys/id_dsa.ppk

To create environment variables in windows, see this link .

Note:
Using environment variables may seem easier for the developer side, but in my opinion, the configuration file is more secure and flexible.

    
26.07.2017 / 17:47