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...
Related
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 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
There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.
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.
I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin