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.
Related
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.
I want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
I have 3 files :
MyApp.jar
start.cmd
start.sh
Here is what's in the start files :
java -cp MyApp.jar -Xms256m -Xmx1024m com.companyname.launch.Launcher someArgs -DsomeParameter=true
As you can guess, I have to start the application by executing the correct start file, depending on the OS I'm using.
Is there a simple solution to get rid of the start files, and have only a system independent executable jar file ?
You can run this from Java.
Have a FirstMain which doesn't depend on anything. e.g. can be run without command line arguments. This main start another java program for the same JAR starting the RealMain with all the command line arguments you need.
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>
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