0

I have a server with Linux Mint OS which has a server-side Maven project that reads some environmental variables.

The environmental variables are declared in /etc/profile.d/my_env_vars.sh with:

export MY_VAR=/opt/my_app 
export PATH=$MY_VAR:$PATH

My server side has a function that when I run it from eclipse (installed in my server) it works fine with no errors.

public File convert( byte [] image) throws IOException {
    String filePath = System.getenv("MY_VAR");
    File imageFile = File.createTempFile("TEMP", "tmp", new File(filePath)); 
...
    return imagenFile;
}

My client-side also is a maven project. It sends a byte[] image to the function above and the server gives it back a result, but I got the NullPointerException error:

java.lang.NullPointerException
java.io.File.<init>(File.java:277)
the second line of my function File imageFile = File.createTempFile("TEMP","temp...

Immediately in my server terminal I verified the environmental variable by echo $MY_VAR and it gives me the correct value (/opt/my_app)

Why does my function work correctly in my server-side but it gives nullPointerException error in the client-side?

I have tried by declaring the environmental variables in other files as ~/.bashrc ~/.profile /etc/bash.bashrc and nothing works. I read in other post to try to edit ~/.bash_profile but Linux Mint doesn't have that file.

I really appreciate any advice.

Veronica
  • 31
  • 4
  • How did you run your server application? java -jar server-app.jar in command line??? First you must determite how your server app run and then double check your .profile. 2nd, add more log into your application to trace. – Bui Anh Tuan Aug 21 '17 at 07:23
  • Thank you for your answer. I run my server-side with Wildfly8. I start it in the terminal by: sudo ./standalone -b 0.0.0.0. Once its up, I can check the app in the browser, and in the terminal I can check any request from the browser or the cliente-side. My client-side it´s working in a raspberry pi. But it does not have any OS login – Veronica Aug 22 '17 at 03:18

3 Answers3

0

On the client, the NullPointerException is thrown because the environment variable is not set correctly and null is passed to the constructor of File.

Put MY_VAR in ~/.profile and notice the file is only loaded once when you login. Therefore, after setting a new environment variable, make sure to log out and back in. If the file doesn't exist, create it.

You can test that it's set using:

$ env | grep MY_VAR
MY_VAR=value
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • Thank you for your help. Actually, I realized I setup my env variables in my user, but I was running my server-side as root. Therefore, the env variables did not appear. – Veronica Sep 01 '17 at 16:00
0

Try putting the commands, which create env variables and run the java application into the same sh script or the same "sandbox". I.e. you can put java command into /etc/profile.d/my_env_vars.sh or create another sh script and put into it the call of /etc/profile.d/my_env_vars.sh and after that run java application.

dimirsen Z
  • 873
  • 13
  • 16
  • Hi, thank you for your answer. Actually, I hava a java.sh in the same path /etc/profile.d/ where I declared $JAVA_HOME. So, I put my env variables inside java.sh before $JAVA_HOME. But I receive the same null error. Any other suggestion? – Veronica Aug 22 '17 at 03:57
0

Can you try putting a println of the variable filepath in your client-side. If it is not printing the path, first of all check if client and server are able to connect to each other or not by using ssh.

I think the problem is because there is no connectivity between the client and server.

Lohit Gupta
  • 1,045
  • 6
  • 11