I have a jar file that I want to launch from a bash script. This jar includes references to an external folder that contains images.
When I am running the jar from command line with the absolute path to the jar, all works OK. The problem appears when I run it from a bash script. Apparently the folder that contains the images is not found.
Launching from command line:
java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar
Bash script:
#! /bin/bash -x
cmd="java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar"
eval $cmd
The directory where are my images are is:
/home/version4/img
The Java code for accesing the images:
String img_header="./img/HEADER.png";
String img_body="./img/BODY.png";
BufferedImage header,body;
header=ImageIO.read(new File(img_header));
body=ImageIO.read(new File(img_body));
The output error:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
Can anyone tell me what I am doing wrong? Thank you.
You should run your bash script from the same directory as you run your jar file. Still, it can be located in another place, the only meaning detail is your working directory. The better way you have is to store the images inside jar, see Getting a BufferedImage as a resource so it will work in JAR file
In any case, you should not use absolute paths to retrieve the images as this solution just destroys any portability of your jar.
Try giving the absolute path of the image (in your java code) if you are running the bash script from a location other than your jar location.
Related
For example, I have someFile.jar. I can run it simply using java -jar someFile.jar.
But I want to create some files (for example .deb file) to install this jar to another machine. This machine may not have jre15 which is required for running this jar. So I want to create some executable file that will contain jar and JVM
I tried to use jpackage --name testName --input . --main-jar someFile.jar --linux-shortcut. This command generates `.deb file but it won't create terminal command testName (to run this jar) but it creates a desktop app that I can run as an application but no one terminal command created. So, how can I create a deb file from the jar with embedded JVM to be able to run it as a command from terminal?
Investigate the content of the deb, or investigate the filesystem once the package is installed. Per default you should find it in /opt/. Underneath that you find a bin directory with only one file. That is the one you can invoke from command line.
JPackage also creates a launcher, which resides in /opt//lib/*.desktop. Check that text and you see that the executable is just the file in the bin directory, which means the GUI will run that exact command when the icon is clicked.
I've made an executable jar file for a terminal game that can be opened by typing java -jar name.jar in the Terminal.
Then I made a .sh file inside the same folder as the jar file to open it by double-clicking the .sh. I asked how to do this here, where people told me to use the following code in the .sh.
#! /bin/bash
DIR=$(dirname "$0")
java -jar "$DIR/game.jar"
This worked for a while, but when I renamed the folder, I realised if I move the folder to a pen drive the whole thing stops working and I get this in the Terminal.
Error: Unable to access jarfile /Volumes/Hard
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So how to find the file path to the folder the .sh and the jar are in, regardless of where it is, what its name is and what drive it is on?
Also, I'm using MacOS Mojave 10.14.4 if that's of any importance.
The error looks like the path does contain spaces, like probably /Volumes/Hard Drive/Users/something. The solution is to quote the command substitution.
Tangentially, don't use upper case for your private variable names.
But of course, the variable isn't really necessary here, either.
#!/bin/sh
java -jar "$(dirname "$0")/game.jar"
Nothing in this script uses Bash syntax, so it's more portable (as well as often slightly faster) to use sh in the shebang. Perhaps see also Difference between sh and bash
You can store the full path of the working directory using the environement variable $PWD, like in this example (done in 5min, it is just to show you how it is works) :
#!/bin/bash
DIR=$PWD
gamePath='java -jar '$DIR'/game.jar'
echo $gamePath
Wherever I will execute this script, it will shows up the working directory even if I change the name of the parent. Let me show you :
You can see that $PWD environnment variable works great.
Now, I will change the directory name from TestFolder to TestFolderRenamed and execute the script again :
So, in your case, change your code as following :
#! /bin/bash
DIR=$PWD
java -jar "$DIR/game.jar"
It should works.
I have a runnable jar-file which I want to start from a batch file. However the jar-file has to be started with a VM-option. The following batch file starts the jar file (in a static way).
java -Djava.security.policy=C:\Users\uname\
\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://C:/Users/uname/Documents/Folder
/anotherFolder/target/classes/ -jar %~dp0jarfile.jar %*
pause
btw: I know that
\src\main\java\rmi\client.policy
is not in a jar file yet, but I'm assuming that everybody has this file structure already on his machine.
However, I want to be able to start the jar file with a relative path, so that every Windows10 (x64) user can use my jar file system-independently. How to achieve that via batch?
Replace each reference to user home C:\Users\... with %userprofile% variable as per this answer explanation.
java -Djava.security.policy=%userprofile%\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://%userprofile%/Documents/Folder/anotherFolder/target/classes/
-jar %~dp0teamFour-1.0-SNAPSHOT.jar %*
or switch to %userprofile% directory with cd before executing java and depend on relative paths.
I made a java project.
The project is....output log message and system.out.println message. just simple.
So I changed into a jar file(the name is LinuxSample.jar).
and I wrote a shell script to run this jar file.
Look at this shell script. (speakee is package name and PrintLinux is main class name)
#!bin/bash
CLASSPATH=/home/tangooc/TANGOOC/test/libs/*
CLASSPATH="${CLASSPATH};/home/tangooc/TANGOOC/test/linux/LinuxSample.jar"
java speakee.PrintLinux
this jar file and this shell script work in Window.
but linux didn't work. I don't know why
this is error message.
Could not find or load main class
Hi Best way to run a java application is to set CLASS_PATH and PATH variable first. If your current jar file depends on external jar files you will face lots of problem. Better set your path variable like below and run the application:-
#!/usr/bin/ksh
export PATH=/usr/java/bin:$PATH
# =/usr/java/bin is your java bin folder
#set environment variable CP with all the jar libraries
CP=/home/flussi/xmlEncoder/encoder.jar
CP=${CP}:/other/jar/somejar.jar
java -Xmx256M -classpath "$CP" "com.myproj.Example"
I made it
I changed the shell script.
CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
LIB_TOTAL=/home/tangooc/TANGOOC/test/libs/*
echo ${LIB_TOTAL}
echo ${CLASSPATH}
java -cp ${LIB_TOTAL}:${CLASSPATH} speakee.PrintLinux
also there is another way.
CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
CLASSPATH=${CLASSPATH}:/home/tangooc/TANGOOC/test/libs/*
echo ${CLASSPATH}
java -cp ${CLASSPATH} speakee.PrintLinux
If the someone like me change the shell script.
and check a line, a line, a line...
I made a batch file that executes commands through a jar:
#java -jar commands.jar %*
This is the file tree:
C:\
Commands\
commands.bat
commands.jar
I have my PATH variable set to "C:\Commands\" so that I can access the commands from anywhere on the PC. I can successfully run the batch file, but for some reason it won't open the jar. When I type
commands
or
commands help
it says
Error: unable to access jarfile commands.jar
I have tried using quotes around the jar name, I have tried moving the jar... nothing works...
%~dp0 gives bat file path so
#java -jar %~dp0commands.jar %*
See call /? for help (even though we aren't using call).
By using ftype, assoc, and editing with setx the variable pathext to add jar files you can just type the jar's name and have it run without a batch file needed.