How to create a dynamic path inside a batch file? - java

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

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 Script on Startup

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>

executing java -jar via classpath vs in the jar file dir

After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar

windows service from batch file failed to execute

i need to execute a batch file as windows service.
For that i had created a batch file.
In this batch file i just add the below code to run a jar file.
java -jar myTest.jar
When i double click on the batch file..no problem .its working fine. It executes the jar file (a java application).
But the same batch file when i used in a windows service on a windows server, its not working.? Its just getting blinked to show the command window and gets closed. None of my code portion inside the jar file gets executed.
Another thing is i had successfully checked this from another windows server. Its working fine there.
Why this strange issue..??Can anyone help me out to solve the issue..
The service is not executed in the same environment as when you run the batch from an interactive Windows session. Make shure in the .bat file that change into the correct (working) directory, even with absolut path (cd \users\my\java\service), and maybe specify the full path to the java.exe. The other server do you mention could have a totally diferent setup of the environment, installed software, etc.
C:
cd \users\my\java\service
"\program files\java\jre\bin\java" -jar test.jar

Categories