• Post Reply Bookmark Topic Watch Topic
  • New Topic

java path problems  RSS feed

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, i made a java programs which uses some files for its execution



I created a jar file and i put the jar file and above mention report file in /opt/myJavaProgram/

my directory tree is like follows
/opt/myJavaProgram/myJar.jar
/opt/myJavaProgram/reports/myReport.jrxml


now is the question
if i run the jar file using java -jar myJar.jar it works fine
and it will NOT work if i use java -jar /opt/myJavaProgram/myJar.jar

i guess that is because working directory related problem. but i cannot figure out how to get rid of it

any idea to solve that?
Thanks in advance!

-supun
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note
if i run the jar file using java -jar myJar.jar it works fine
and it will NOT work if i use java -jar /opt/myJavaProgram/myJar.jar

supun@supunz /opt/myJavaProgram $ java -jar myJar.jar // this works fine

supun@supunz ~ $ java -jar /opt/myJavaProgram/myJar.jar //this is not
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because . is the directory where the program was started from - /opt/myJavaProgram in your first example, and your home directory (~) in the second.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:That's because . is the directory where the program was started from - /opt/myJavaProgram in your first example, and your home directory (~) in the second.


Thanks Rob! 

But i was looking for something like to force java or program to consider relative path from it's jar location.

Anyway thanks for your time Rob

-supun
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you running your work from /opt rather from a home directory?
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you running your work from /opt rather from a home directory?


I need to create a desktop shortcut entry and here is how its looks like

[Desktop Entry]
Version=1.0
Name=My Software
Exec=java -jar /opt/myJavaProgram/myJar.jar
Terminal=false
Type=Application


anyway Thanks Campbell Ritchie!
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what is wrong with keeping your .jar somewhere in /home? Your desktop link shou‍ld work nicely however.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . and what is wrong with keeping your .jar somewhere in /home? Your desktop link shou‍ld work nicely however.


found a working solution with the help of a friend (Rob Brew)

[Desktop Entry]
Version=1.0
Name=My Software
Exec=bash -c 'cd "/opt/myJavaProgram" && java -jar myJar.jar'
Terminal=false
Type=Application


Thanks Rob & Campbell Ritchie!
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think there's supposed to be a "working directory" directive for that desktop link file, so that would eliminate the need for the "cd" command in the Exec directive.

Note that "/opt" is supposed to be a shared system directory, so it's not er, optimal, for use as a single-user app.

In fact, if you want multiple users to have the ability to use the same app, you can put the executable in /opt - preferably a subdirectory of /opt, but you should use the Java Preferences system if you need per-user files.

Probably even better would be to put the Java app under /usr/local (or /usr/local/bin*), use Java Preferences for per-user data, and put any common data files under /var/lib. And while you're at it, add a shell script to run the java -jar command so that the Exec is more Linux-friendly.


*Note that there are attempts being made to reduce the number of "bin" directories in Linux, so check current recommended practices in the LSB.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Actually, I think there's supposed to be a "working directory" directive for that desktop link file, so that would eliminate the need for the "cd" command in the Exec directive.

Note that "/opt" is supposed to be a shared system directory, so it's not er, optimal, for use as a single-user app.

In fact, if you want multiple users to have the ability to use the same app, you can put the executable in /opt - preferably a subdirectory of /opt, but you should use the Java Preferences system if you need per-user files.

Probably even better would be to put the Java app under /usr/local (or /usr/local/bin*), use Java Preferences for per-user data, and put any common data files under /var/lib. And while you're at it, add a shell script to run the java -jar command so that the Exec is more Linux-friendly.


*Note that there are attempts being made to reduce the number of "bin" directories in Linux, so check current recommended practices in the LSB.


Thanks Tom!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!