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.
Related
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.
I'm trying to run a script from an Amazon Linux machine. The script invokes checkstyle like this (in a script called eval.sh):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. Both checkstyle-6.15-all.jar and checks.xml are where they should be.
What could cause this problem?
Change "${CHECKSTYLE}" to ${CHECKSTYLE} (without the quotes).
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar.
When you envoke "${CHECKSTYLE}" the shell thinks that is the command you are running. There is no such file name with the spaces and options have you have included there. If you envoke it simply as ${CHECKSTYLE} (drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process.
Suppose I have some folder in a directory:
- MyApp
- lib
- myapp.jar
The location of the MyApp directory is supposed to be stored in an environment variable, like APP_HOME. I would like to add a bin folder that contains two commmand-line executables that launch the java program, one for Windows and one for Unix-based OSs. I already know that one file would just be called myapp and modified with chmod +x, and the Windows one would be named myapp.bat.
What I am unsure about is what the contents of these files would be. As said, both would run the jar file with a custom command line command whose arguments are passed to the main method, as shown below:
>myapp -debug key=value moreargs...
EDIT: How would I go about creating this environment variable, from Java code?
You can pass command line arguments to the executable by adding $* at the end of the command in the Unix shell script, and %* for the Windows batch file:
java -jar $APP_HOME/lib/myapp.jar $*
I am trying to launch a jar file on Windows Server 2008 R2 startup.
I tried to add a key\value to
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
I tried as a value several alternatives:
java -jar c:\jcm\jcm.jar
"java -jar c:\jcm\jcm.jar"
c:\program files(x86)\java\jre1.8.31\bin\java.exe -jar c:\jcm\jcm.jar
"c:\program files(x86)\java\jre1.8.31\bin\java.exe -jar c:\jcm\jcm.jar"
"c:\program files(x86)\java\jre1.8.31\bin\java.exe" -jar c:\jcm\jcm.jar
But none of them launched the program.
Can you advise please?
EDIT: Fixed exe to jar of course
You probably have a problem in the command line. I think it should be something like that last row that you have used but with jar and not exe.
"c:\program files(x86)\java\jre1.8.31\bin\java.exe" -jar c:\jcm\jcm.jar
You just need to test it in command line first and if it works it will work in the registry. You can also make a .bat file to start the java program and copy that to windows Startup Folder instead of using the registry.
The startup folder you can find here:
http://windows.microsoft.com/en-us/windows/run-program-automatically-windows-starts#1TC=windows-7
If it is Windows 10 then you may try this way-
Create a batch file e.g. (my-batch.bat with below statements.)
#echo off
title my-app batch script!
echo my-app welcomes you!
:: If your JAR contains GUI then use-
start javaw -Xmx200m -jar D:\Softwares\JAR\my-app-v1.0.jar
:: Otherwise use-
start java -jar D:\Softwares\JAR\my-app-v1.0.jar
Copy this my-batch.bat file at
C:\Users\pc\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Restart your PC to execute your JAR on startup.
Now, You may check your running JAR using Command Prompt with below commands-
1. jps -l
Output will look like-
8120 sun.tools.jps.Jps
13276 D:\Softwares\JAR\my-app-v1.0.jar
2. tasklist /V
I'm trying to run a batch file that included multiple jar files, Batch file includes 3 Jar files which executes one after another in one window, My batch file is working correctly for one record which fetches data from excel sheet.
Consider a scenario in which i have 5 records and i wanted to run the batch file in the manner like for 1st record->1st jar prog executes then 1st record->2nd jar file and finally 1st record->3rd jar file executes. Then this loop continues for the second record and likewise. Could anyone please help me to modify the below script which runs in loop and i want to save a executed results in a separate text file.
My script is below:
REM Run first and finish ...
java -jar first.jar
REM .. then start number two.
java -jar second.jar
REM .. then start number three.
java -jar third.jar
Kindly help!
You cloud do something like this that waits until the execution of one jarfile is done.
#echo off
java -jar 1.jar
pause
java -jar 2.jar
pause
If you want to run the jars sequentially, you can write a .bat file containing the following;
#echo off
java -jar first.jar
java -jar second.jar
java -jar third.jar
If you want to tun the jars simultaneously, you can write the .bat file as follows;
#echo off
start java -jar first.jar
start java -jar second.jar
start java -jar third.jar
START command will start running the jar in a new window.
You'll need something like
FOR %%A in (1 2 3 4 5) DO (
java -jar first.jar
java -jar second.jar
java -jar third.jar
)
That should execute the three jar consecutively five times. I didn't actually test this, but it should give you the idea. Here is an article on FOR loops syntax in batch files: http://www.robvanderwoude.com/for.php