Running Script on Startup - java

Essentially what I'd like to achieve is add a .jar file to the startup of my Linux computer. I created a script that would run the file and then tried adding that script to run when the system boots up.
I have a .java file on my desktop named Box.java. The file contains no errors and I manually compiled it and it was working fine. I then created a script on my desktop called start.sh and it's contents are
#!/bin/bash
javac /home/maple/Desktop/Box.java
So what this should do is compile the java class and the result would be a class file on my desktop called Box.class
I then created a file in /etc/init.d/ and it is called **start_java* it's contents are
#!/bin/sh
home/maple/Desktop/start.sh
I then opened up terminal and did
chmod +x /etc/init.d/start_java
I know the sh file will compile a java file and not run a file, in the completed copy I will be doing it with a jar file. How can I add that jar to startup without using a 3rd party software or any pre-installed programs?
EDIT: The current way I have it is not working.

You just want to do the same thing you already did, but changing the content of start.sh to:
#!/bin/bash
java -jar <filename>

Related

Convert java (with params) to exe

Using Eclipse I've created a Java project with one single class that needs an argument.
In my PC where I have Java installed, I run this:
java -jar test.jar %arg1%
I can put this in a batch file and it runs successfully. The batch has the next lines:
echo off
set /p arg1=Enter path where file is:
java -jar test.jar %arg1%
pause
How can I generate this but in an exe file?
I need that the exe file ask for a path and execute the jar even thought the PC doesn't have Java
P.D. In some forums it's suggests to use launch4j but it doesn't allow dinamical params.

How to get jar name into startup script within bat(batch) file

I have a bat file I run through command prompt to deploy a java app locally for local testing on my machine:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-version.jar
exit
To run this bat file, I use the following command:
start batFileName.bat
However, the next time the version changes on this jar, the bat file will not work, because the version is out of sync. This results in myself having to change my bat file each time the version is updated.
Is there a way to pass in a the version when I run the start command through command prompt to use as the jar name? This way when I run my bat file, I can just pass in the name of the jar at that time to run the java application? If so how would I pass that version into the bat file and how would I use that parameter?
In your script, replace the version part of the jar file name with an argument replacement parameter:
start java -server -AnotherParameter -AnotherParameter -jar path\to\jar\appName-%1.jar
Do not start the program using java -jar . Change the start up script
include the folder where you jar file is present into class path with wild card, like:
java -cp path\to\jar*
call the main class in your jar file. I suppose the main class does not change so often as versions of the jar file?
The whole command line will look like this:
java -cp path\to\jar* com.something.foo.bar.Main
JVM will load your jar whatever its name is, and will find the main class and will start it if it has "main" method.

Running an uploaded executable Java program with script

I have a simple .sh script, which runs a Java program:
#!/bin/bash
java -jar Test.jar
However, the Test.jar is referencing a .jar file in the folder. I would like to know, whether it's possible to execute a file, that has been previously uploaded to a website. Reason: The script is sent onto (and then ran from) a server, to which I do not have access, which is why I cannot upload the said .jar file onto it.
I've tried with wget, but that command only downloads the file, rather than uses the said file in the script.
What about
#!/bin/bash
wget http://server/Test.jar
java -jar Test.jar

How to create a dynamic path inside a batch file?

I created a command line program in java that will be deployed. Since it has no windows it can't be run by double clicking. I then created a batch file that will run the jar but since the program will be deployed they will have a different path than me.
How do I go from:
java -jar E:\Projects\Java\SystemFileQuery\SFSQ.jar
to:
java -jar ...\SFSQ.jar
Just deploy the SFSQ.jar file to the same directory as your batch file and then you can use
java -jar SFSQ.jar

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