How can i specify the Working directory from bash script - java

I want to run my java algorithm from bash script. When i run my program from Netbeans i can specify my Working directory from application. So, how can i specify the Working directory from bash script ?

In Bash, you can just cd to the directory you'd like to work from. So, for example, if your app lives in ~/bin/app.jar, you'd have something like the following:
#!/bin/bash
WORKING_DIR=$HOME/lib # whichever directory you want to work from
cd $WORKING_DIR
java -jar ~/bin/app.jar
The trick is using an absolute path to your executable, or in this case, an absolute path to your Jar file.

Related

How to find file path to a bash file when the folder is renamed or moved to a new drive?

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.

How to run jar file on shell script in linux terminal?

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...

How to use a .jar from Command Line without typing full path

I downloaded example.jar and I can type java -jar example.jar from within the directory where it is located and it works.
The problem is that I need to be able to call it from elsewhere without typing the full path. Is it possible?
I tried adding it to $CLASSPATH like this:
export CLASSPATH=$CLASSPATH:/Path/to/Directory:/Path/to/Directory/example.jar with no success.
Yes. Option 1. Using the CLASSPATH you have set, however you would have to specify the fully qualified main-class from the jar
java com.mypackage.MyMain
As long as com.mypackage.MyMain is on the CLASSPATH and contains a valid main method, that will run it.
Option 2. Create a bash shell script to run it (note that this is really providing the full path to the java command)
#!/usr/bin/env bash
export JARFILE="/Path/to/Directory/example.jar"
java -jar $JARFILE

Arbitrary install a .jar file using a regex

So I was wondering in bash is there a way to install a jar file like this:
java -jar /opt/install.*.jar
I will only have one .jar file in the /opt directory with the start of the name called install followed by some versioning e.g install6.5.3.jar.
Is there a way to do this or would i have to do an ls .jar and extract the name that way?
Bash would automatically expand that *, assuming you've not disabled globbing.
For example, if you've got a directory like:
mkdir /tmp/foo
touch /tmp/foo/bar.jar
and you executed
java /tmp/foo/*.jar
then bash would execute:
java /tmp/foo/bar.jar
So you don't have to do anything special.

Writing a Unix installer for a Jar file

I have written a Java application, and I have created an executable Jar file that successfully runs my application with java -jar myJar.jar. I have an executable shell script called launchMyProgram that wraps the launching of this Jar (and offers various flags like --help etc.). My target output directory looks like this:
$ ls /path/to/MyProject/target/
archive-tmp/ classes/ myJar.jar
What is the standard method for me to write an installer for my Unix-only application? I assume that I would be correct to drop the launchMyProgram executable in /usr/local/bin. But where do I put the Jar file? Should I make my own subdirectory somewhere for my program's files? Do I need to copy the classes directory from the above output into the same directory as the Jar? This will run via a Makefile so of course users may override my choices.
Basically, I want a user to be able to run make && make install, and be able to run my application with launchMyProgram, and I want place my files (one jar, a 'classes' folder, and a shell script) in the most typical places possible.
One of the best ways to do it has been reinvented many times but is unfortunately not yet standard.
Since a JAR is a ZIP file which is allowed to have an arbitrary prefix, you can prepend a launcher shell script to your jar, mark it executable, and treat that as a standalone binary.
$ echo '#!/bin/bash' > launchMyProgram
$ echo 'exec java -cp "${0}" com.example.program.Main "${#}"' >> launchMyProgram
$ cat myJar.jar >> launchMyProgram
$ chmod +x ./launchMyProgram
$ ./launchMyProgram
Hello, world!
See Simple & Easy Executable Jars for more details.
You should be able to pack everything in the classes/ folder into your jar and still have things work.
Also, if you want to provide RPMs or DEB packages or something for users, fpm makes that really easy.

Categories

Resources