• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

java path problems

 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you running your work from /opt rather from a home directory?
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80644
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and what is wrong with keeping your .jar somewhere in /home? Your desktop link shou‍ld work nicely however.
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Saloon Keeper
Posts: 28663
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic