Autostart .jar executable is not working - java

I want to start my Java Application (a Discord bot) when I boot my windows 10 PC.
To do that. Also, I have a bat file in my Autostart folder with the following:
start javaw -Xmx200m -jar D:\Local\bot.jar
EXIT
The main of the Java Code has the following:
public static void main(String[] args) {
try {
BotListener NL = new BotListener();
jda = new JDABuilder(AccountType.BOT).addListener(NL).setToken(BOT_TOKEN).buildBlocking();
} catch (LoginException | IllegalArgumentException | InterruptedException | RateLimitedException e) {
e.printStackTrace();
}
I've tried shutting it down and creating a new jda after a delay in a thread, both with the delay in the main thread and in another thread class. I also tried shutting it down and then running the bat file again.
When I run the bat file it works. Only when it's run automatically on startup it fails. I am assuming it's a problem with Java or using a .bat and a .jar or something because restarting the jda worked when I run it myself.

Related

How to execute 'cd' command and then a linux command in java

I have directory /tmp
then I need to execute cd and go to that folder.
Then I need to execute ./executeScript
Preparation
To start solving the problem, I created a directory /home/vulpini99/tmp. In this directory, I created the bash-script test.sh, which will open firefox for us:
firefox
Then I created a java-file named LinuxCommand.java in the directory /home/vulpini99.
Main part
cd is just an internal shell command and not an executable program, so I suggest to just use the full path of the bash-script. So the command we want to execute is
bash /home/vulpini99/tmp/test.sh.
In Java, you can use Runtime for this purpose:
import java.io.IOException;
public class LinuxCommand {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("bash /home/vulpini99/tmp/test.sh");
}
catch(IOException e) {
e.printStackTrace();
}
}
}

How do I run a program at startup on Raspberry Pi AFTER the GUI has loaded?

Okay, so I have this simple java program:
import java.awt.AWTException;
import java.awt.Robot;
class Program
{
public static void main(String[] args) throws AWTException
{
Robot robot = new Robot();
// do some things with the robot
}
}
I want to run this program on my Raspberry Pi at startup so I've put it in a shell script which I've called at the end of the /etc/rc.local file. This is my shell script:
cd /home/pi/Desktop
java Program
Whenever my Raspberry Pi boots up, my program throws an exception saying Can't connect to X11 window server using :'0.0' as the value of the DISPLAY variable as soon as it tries to instantiate the Robot class. I later found out that this is because the GUI has not yet loaded when my program is being executed so I've put a delay in rc.local. These are the last lines in rc.local:
sleep 60s
sudo sh /home/pi/Desktop/launcher.sh &
exit 0
Although the program starts running after the GUI has loaded, it still throws out this exception. I've tried:
Using .bashrc instead of rc.local
Putting a delay inside my launcher.sh script before calling my actual program
Calling another shell script that had a delay at the beginning and then called launcher.sh
I had no success and have depleted all my ideas and don't know where else to search for the solution to this issue.
To start an application when GUI starts, you should add .desktop file to the autostart directory.
So, in ~/.config/autostart directory, create my_script.desktop file (substitute my_script with whatever you want).
touch my_script.desktop
Edit it (nano my_script.desktop) so it looks like this:
[Desktop Entry]
Name=put_name_here
Exec=type_command_to_run_here
Type=application
Terminal=true/false (true if you want it to run in terminal)

Running .JAR file from java code (netbeans)

Im trying to run a jar file from java code, But unfortunately does not success.
A few details about the jar file:
The jar file located in a different folder (For example - "Folder").
The jar file using a files and folders are in the root folder (the same "Folder" i mentioned above).
What im trying to do so far:
JAR file project.
In netbeans i checked that the main class are defiend (Project properties -> Run -> Main Class).
Other JAVA program
Trying to run with the command:
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar");
&&
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar" "C:\\Software");
The jar file opened well, But he doesnt know and recognize his inner folders and files (the same "Folder" i mention above).
In short, it does not recognize its root folder.
Trying to run with ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "javaw", "-jar", "C:\\Software\\program.jar");
pb.directory(new File("C:\\Software"));
try {
pb.start();
} catch (IOException ex) {
}
In Some PC's its works fine, But in other pc's its not work and i got an error message: "Could not find the main class"
** Offcourse if i run the jar with double click its works.
So how can i run a jar file from other java program ?
Use this variant of .exec where you specify working folder as the third argument. (In your examples, you always only use one argument.)
exec("javaw -jar "C:\\Software\\program.jar", null, "C:\\Software");
You can try to call it something like below. There are 2 types of calling it.
public class JarExecutor {
public static void main(String[] args) throws IOException, InterruptedException {
//This is first way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-jar" ,"C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar"});
//This is second way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-cp","C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar","com.shiva.practise.FloydTriangle"});
proc.waitFor();
BufferedInputStream is=new BufferedInputStream(proc.getInputStream());
byte[] byt=new byte[is.available()];
is.read(byt,0,byt.length);
System.out.println(new String(byt));
}
}

JavaFX - Virtual Keyboard Doesn't Show When the App Jar is Generated

I have created a JavaFX application in IntelliJ14.14 that will use the JavaFX Virtual Keyboard. I have add the following properties in the mainApp class controller:
public static void main(String[] args) {
System.setProperty("com.sun.javafx.isEmbedded", "true");
System.setProperty("com.sun.javafx.touch", "true");
System.setProperty("com.sun.javafx.virtualKeyboard", "javafx");
launch(args);
}
When I run the app from IntelliJ everything works fine. The virtual keyboard works perfectly.
But when I generate the Jar file of the application from Build -> Build Artifacts... -> Build and execute it, the keyboard never shows because the VM options are not being set.
It's something I'm missing...?
Thanks in advance...
EDIT
I have found a way to make this work, running the file from the cmd with this command:
java -Dcom.sun.javafx.isEmbedded=true -Dcom.sun.javafx.virtualKeyboard="javafx" -Dcom.sun.javafx.touch=true -jar myApp.jar
However I want to make this just executing the Jar file...
EDIT
There is another nearby way to accomplish what I want...
Create a file .bat in the same folder of the jar and put in it:
start javaw -Dcom.sun.javafx.isEmbedded=true -Dcom.sun.javafx.virtualKeyboard="javafx" -Dcom.sun.javafx.touch=true -jar myApp.jar
So when tha .bat file is executed and the jar file is started, the system properties are loaded correctly...
public class MyApp {
public static void main(String[] args) {
if (args.length == 0) {
try {
// re-launch the app itselft with VM option passed
Runtime.getRuntime().exec(new String[] {"java", "-Dcom.sun.javafx.isEmbedded=true", "-Dcom.sun.javafx.virtualKeyboard=\"javafx\"", "-Dcom.sun.javafx.touch=true", "-jar", "myApp.jar"});
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.exit(0);
}
// Run the main program with the VM option set
//...
//...
}
}

Running shell script on tomcat7

I have been breaking my head for two days trying to fix the file permissions for my tomcat7 server. I have a library class (.jar file included in myapp/WEB-INF) which needs to run a shell script. The library is written by me and works fine within NetBeans ie. no hassle in creating,reading and deleting files. That is because NetBeans runs the program as blumonkey(my username on my Ubuntu System). But when I import this into tomcat and run it, tomcat "executes" the command, produces no definite output, tries to check for a file(which will be generated when the script succeeds) and throws a FileNotFoundException.
More Details as follows:
Tomcat7 installed using apt-get, has its data in 2 locations - /var/lib/tomcat7 with conf and webapps folders and /usr/share/tomcat7 with the bin and lib folders
The user uploads a .zip file which is stores to /home/blumonkey/data. Rest of the program runs on the documents stored here. All new folders/files uploaded by tomcat have, obviously, tomcat7 as the owner.
I have tried things like changing the ownership to blumonkey, adding tomcat7 to blumonkey user group but none of the methods worked (Somewhere around here I probably messed up changing permissions carelessly :/ ). Apparently tomcat7 is unable to process on the files it owns.(How can this be?).
The script works when I run it in the terminal. But it doesn't work when I do a sudo -u tomcat7 script.sh, ie run it as tomcat7. It just exits with no message. I doubt that this it what is happening as I have tried to debug by redirecting the errors and outputs in ProcessBuilder but they came empty.
Any help regarding how to fix the issue and get the script running would be greatly appreciated. Please comment if you need any more info.
The code for script execution
private static void RunShellCommandFromJava(String command,String fn, String arg1,String arg2) throws Exception
{
try
{
System.out.println(System.getProperty("user.name"));
ProcessBuilder pbuilder = new ProcessBuilder("/bin/bash",command,fn,arg1,arg2);
System.out.println(pbuilder.command());
pbuilder.redirectErrorStream(true);
Process p = pbuilder.start();
p.waitFor();
}
catch(Exception ie)
{
throw ie;
}
}
The command which needs to be executed
"/bin/bash /abs/path/to/script.sh /abs/path/to/doc/in/data-folder maxpages=30 maxsearches=3"
PS : I have followed this question but it didn't help. I also tried other options like Runtime.exec(), bash,/bin/bash/ and /bin/bash/ -c, some of them don't work at all, others give no results.
Try to use Runtime and check standard error to find out what was the problem (probably permissions or paths):
// run command
String[] fixCmd = new String[] { "/bin/bash", "/abs/path/to/script.sh", "/abs/path/to/doc/in/data-folder", "maxpages=30", "maxsearches=3" };
Process start = Runtime.getRuntime().exec(fixCmd);
// monitor standard error to find out what's wrong
BufferedReader r = new BufferedReader(new InputStreamReader(start.getErrorStream()));
String line = null;
while ((line = r.readLine()) != null) {
System.out.println(line);
}

Categories