run executeable jar with a linux script - java

I wrote a little java program and now I want to execute this .jar file from a .sh script.
my script:
#! /bin/bash
java -jar /var/spool/sms/sentSMS.jar
then i run the command: sudo bash sentSMS.sh
an get following error:
ERROR: Unable to access jarfile /var/spool/sms/sentSMS.jar
I am using a Raspberry with raspian-jessie, if this important to solve it.
Sorry, but I'm new in scripting with linux.

Take into account that the user must have at least READ permissions on that file.
Also, as you say you are new in linux, make sure the name is correct. sentSMS.jar is different from sentsms.jar

Related

sh: java: permission denied tryinng to execute .jar file

My OS: SLiTaz 32 bits, last version.
I installed java in /usr/bin/java/jre-8u171-i586
I run the terminal as root, cd to where my executable jar file is, and type:
java -jar desktop-1.7.2a-1.jar
I get:
sh: java: Permission denied
What is the cause of this and how can I execute my .jar file?
Also know that this file is a game, more specifically "Pixel Dungeon".
Edit: when I execute java -version, it again says permission denied. I must say that to execute these commands, I run the terminal as my own user, Flavius, then do su, enter password and from there I execute all commands.
Edit2: To install java I uncompressed the tar.gz file I got from the official java site using a website, because I didn't know how to uncompress it by commands, then I moved java files to /usr/bin and then I tried just double clicking the .jar file to execute it, but it asked what program to use to execute .jar files, and I didn't know how to fin java in that menu so I tried using commands...
It seeem that java binary does not have execution permission. Executing chmod a+x /usr/bin/java/jre-8u171-i586/bin/java might solve the issue.
But it something wrong about how you installed java. I would try to install it using the package manager.

Can't run java from PHP's exec

I've been trying to run a JAR file that would read from input.txt and write to output.txt this way in console:
java -jar file.jar input.txt output.txt
And it works 100% fine on my machine. I need to run it inside a php script, and this code works 100% fine for me (Mac OS, php built-in server):
exec("java -jar file.jar input.txt output.txt");
But once I deploy it (CentOS server) where the exec function is allowed, it fails, it returns an empty string and the jar does not work, running it directly from shell is OK.
How can I fix that?
Thanks in advance!
The problem with your exec() is that PHP doesn't know where Java is on the server. Update your command to specify the full path to the Java executable and it should work, though you should also use full paths to the jar and text files while you're at it.

Java JAR file does not execute in startup script in Ubuntu 14.04

The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.

Java -cp on linux

I made a program which runs fine on windows. When I moved it over to CentOS, I got this error:
Error: Could not find or load main class org.wbc.WBCController
This is the file setup and .sh on linux:
And this is the file setup and .bat on windows:
Does anybody know what the problem is, and how I can fix it?
Java will respond with this error even if it cannot find the file wbc.jar. I am guessing that that is your problem. You might want to see that your are executing the shell script from within the right working directory.
Check to see if you can run wbc.sh from the console or put this in wbc.sh to make sure it searches for the jar in the same directory as the shell script:
#!/bin/sh
java -cp `dirname $0`/wbc.jar org.wbc.WBCController

Run java class from single command using makefile

I have a homework assignment in Java that is tested using the commands:
make
./<program_name> <arguments>
my make file compiles my java program successfully, but how can the program be run without using the command:
java <program_name>
I have investigated how to convert a .jar into an .exe but I am convinced that is not the answer I am looking for.
I believe the test is run on a Linux machine. Is there something I can include in the make file to cause the command
./<program_name>
to run a compiled java class?
Without converting the java program in a native executable file, that will be different for linux, for windows and any other platform (so you will loose Java portability), the only thing you can do is to create a launch script.
On *nix system you can create a bash script and on windows a batch script. Then in this script you have to call java <program_name>.
With the script you are now able to launch your application with a single command.
For example on unix you can create myapp.sh:
#!/bin/bash
java -classpath bin com.test.YourApp $*
and make this script runnable with command
chmod a+x myapp.sh
in this example when you write myapp.sh command you launch your Java class com.test.YourApp using the folder bin as classpath.

Categories