15

I have a necessity to read some properties from a file located in local etc/myconfig-config/ folder. I need to give this file path in the command line. I have given it as mentioned below. But there is an error and it displays like

Error: Could not find or load main class test-tool.jar. 

the command given is

java  -cp -DconfigDir=/etc/myconfig-config/ test-tool.jar 
service.ScriptGenerator $clinic_count $client_files_count 

can anybody please help me to resolve this.

thanks

1
  • 1
    Note that you are wanting to read a property at runtime, not an environment (System.getProperty("xxx") vs System.getenv("xxx")). Commented Aug 2, 2022 at 1:27

3 Answers 3

20

Try giving following command,

java -DconfigDir=/etc/myconfig-config/ -cp test-tool.jar service.ScriptGenerator $clinic_count $client_files_count 

Note that the given parameters should be accessed from code as follow:

System.getProperty("configDir")
Sign up to request clarification or add additional context in comments.

5 Comments

It work Sathish but when I put System.getenv("configDir") it prints null. Could you give a solution to this.... thanks
Hi, use System.getProperty("configDir") insteadof getenv() method.
Thanks a lot Sathish it works.... I ask this question if it is not a disturbance to u. When I run it through IDE the System.getenv("configDir") works fine. But when i run it through terminal using above command the System.getProperty("configDir") working fine.. I cannot think why is this...
You are welcome Dilan, I did not get what is not working. Can you please explain? I'll try to help if i know the answer. Thanks !
What dilan suggests set a SystemProperty in your JVM. Not an environment variable. Hence you cannot use it in your code via "getenv()" method. See this blog post to understand better how those two variables work for java applications homeofthewizard.github.io/secrets_in_java
4

Try this command

export VARNAME='variable-value'

Then run your Java main class from the same command line.

Comments

1

Your command should be like

java  -cp test-tool.jar -DconfigDir=/etc/myconfig-config/ service.ScriptGenerator $clinic_count $client_files_count 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.