Using cmd in a java program - java

I am trying to send differents commands into a java program to start my solr server
My problem is that I a trying to send first the command
cd ~/Desktop/PPE/solr-4.10.3/example
and then
java -jar start.jar
Using the line
Runtime.getRuntime().exec(cmd);
Bug I get an error no such file available. Do you have any idea ?

java -jar /path/to/file/start.jar
you have not given the path of the jar file. This is why it is giving this error, and I hope you have java in your environment variables.

Related

run executeable jar with a linux script

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

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.

Connecting to MySQL database using Java in command prompt

Since I don't have netbeans right now, I am trying to connect to MySQL database from my Java code through command prompt. But it it is not taking the mysql-coonectivity.jar file.
Does anyone know any way to run my program??? Please help.
Try executing the program as it is windows OS
java -cp .;path\of\your\mysql-connector-java-5.1.6.jar className
In case you don't have the mysql-connector-java-[version].jar get it from here
You need to add your MySQL connector jar file while compiling and running your program, you can do it the following way,
To compile :
javac -g -cp mysql-coonectivity.jar; YourFileName.java
To Run
java -cp mysql-coonectivity.jar; YourMainClass
NOTE: The above written syntax assumes that your jar file is present at same location as of your program.
To execute program in Linux (Ubuntu) OS follow the below command format:
java -cp .:/usr/share/java/mysql-connector-java-8.0.25.jar MainProgram
Note:
Assuming you are in the directory where MainProgram.class is exist
After .(dot -- this is for current directory) there is :(colon) as a separator in Linux

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

Categories