I want to automatically run a Java application under Windows at startup time, but not depending on a specical Windows or a special JRE-version.
Generally, a good way to run programs at Windows login time would be to add a registry entry under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
But it seems as if the system PATH is not evaluated when Windows tries to run the programs listed in the registry's RUN section.
So these both entries do work:
"C:\Program Files\Java\jdk1.8.0_111\bin\javaw" -jar c:\path\to\java.jar
"C:\ProgramData\Oracle\java\javapath\javaw" -jar c:\path\to\java.jar
But this one doesn't come up:
javaw -jar c:\path\to\java.jar
Now, the latter works when manually entered in a console window because there the required PATH exists and is evaluated:
PATH=....;C:\ProgramData\Oracle\Java\javapath;....
Now, I want to have the registry entry added from an automated installer programm and I would like to have it formed "windows-system-independent" (from Vista up to Win 10, and furthermore not depending from the version of the installed JRE). So I do not know a general path to the JRE and therefore I cannot add a specific RUN-command to the registry.
Does anyone know how to add JRE-calls system-independent correctly to the registry's RUN-section?
I've already tried with a command-file, e.g. "startup.cmd" with content java -jar file.jar there, have added a call of this file to the registry, and yes that worked, java was found, the application was started, but when using this method I have an annoying black command terminal window staying open all the time while the java app is running.
Thanx,
Tombo
Change your startup.cmd, from
java -jar file.jar
to
start java -jar file.jar
This will launch the java.exe in a new process an let your batch executor terminate.
Related
From a Java application I want to run another Java application on the same Java installation but in a separate process.
AFAIK for a new process I would use ProcessBuilder to run a command like
java -jar my.jar
but what if java is in a different directory, or should be java.exe since we are on Windows, or java.exe has some other name since the first application was jlinked and jpackaged?
Edit: What I learned meanwhile is that a jpackaged application comes with a native executable that sits in front of the JVM but passes all arguments to the application. That means it is no longer possible to specify an alternative jar to be executed, and some other mechanism is necessary.
If jlink image used within jpackage based apps is built without using the --strip-native-commands flag then the runtime image will contain bin/java (or bin/java.exe and bin/javaw.exe on Windows).
This will mean that you should be able to determine a path to launch a new JVM which works inside or outside of jpackage apps just based on the current runtime's java.home and by appending extra VM arguments in normal way for the classpath and main / module of the other application.
On Windows the environment property echo %PATHEXT% usually contains .EXE so the following should specify the java executable for the current runtime without needing to add the correct file extension on Windows:
String java = Path.of(System.getProperty("java.home"),"bin", "java").toString();
You can test above inside your jpackage and non-jpackaged app with a simple one-liner (note that this is not proper way to use ProcessBuilder):
new ProcessBuilder(java, "-version").start().getErrorStream().transferTo(System.out);
Obviously, the above is no help if you wish to determine whether to use Windows console enabled java.exe versus non-console javaw.exe.
I have a Java application from one of our suppliers. After installing the application as Admin, I try to open the application as Admin, but nothing happens. I have gone to where the program icon points to, and it goes to a Javaw.exe file:
"C:\Program Files (x86)\Infra12\jre\bin\javaw.exe" -jar Infra12.jar
If I run this Javaw.exe file directly as Admin, nothing still happens. By nothing, I mean there are no windows that open at all.
I have run sfc /scannow and it found nothing.
I didn't have Java itself installed, so I thought maybe that was the issue. So I installed Java (Java 8 Update 191), uninstalled the Java application then reinstalled it, still nothing happens when I try to open it.
I can't go back to the supplier, as they don't support the software any more - it is used to communicate with infrastructure equipment.
Any ideas how I can get this application to work?
Edit
Not sure if this makes a difference, seems to refer to some variables - maybe that can help with opening it?
This is because javaw points to the Java Virtual Machine. If you want to run the application - find the .jar it uses and then double-click that. Or use javaw to run said application:
[WIN] + [R]
Type "cmd"
Enter: javaw -jar path-to-your-jar-file
javaw by itself will do nothing.
EDIT: You need to use the -jar option.
I'm trying to determine the Tomcat install directory when it is started from startup.bat in Windows.
It is easy enough to determine where tomcat7.exe is running when Tomcat is running as a service, but I'm not sure how to do it when it's started with the script. I know java is running when Tomcat is started from the script, but the executable path is for the java jre. Is there something I can do to find where catalina is running based on java?
Assuming you know the location of startup.bat, then just go two folders above and you're done.
Assuming you only have a shortcut to this file, then you would need to retrieve a list of processes explaining the application and the location of the files they're using, similar like ps aux command from Unix based OSes. Fortunately, Windows have such thing as well. From this great Q/A: Is there a command in Windows like ps -aux in UNIX?, more specifically, this answer, the way to find the location of tomcat in Windows is to execute the wmic application (through CMD if you want) and write process command, this will provide a list of the current applications running and their parameters. For example, I initialized Tomcat from startup.bat file and got this result using the commands above (single line):
java.exe "C:\Program Files\Java\jdk1.7.0_40\bin\java" -Djava.util.logging.config.file="<TOMCAT_HOME>\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs="<TOMCAT_HOME>\endorsed" -classpath "<TOMCAT_HOME>\bin\bootstrap.jar;<TOMCAT_HOME>\bin\tomcat-juli.jar" -Dcatalina.base="<TOMCAT_HOME>" -Dcatalina.home="<TOMCAT_HOME>" -Djava.io.tmpdir="<TOMCAT_HOME>\temp" org.apache.catalina.startup.Bootstrap start
Here's the same result but splitted in several lines to ease readability:
java.exe "C:\Program Files\Java\jdk1.7.0_40\bin\java"
-Djava.util.logging.config.file="<TOMCAT_HOME>\conf\logging.properties"
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.endorsed.dirs="<TOMCAT_HOME>\endorsed"
-classpath "<TOMCAT_HOME>\bin\bootstrap.jar;<TOMCAT_HOME>\bin\tomcat-juli.jar"
-Dcatalina.base="<TOMCAT_HOME>"
-Dcatalina.home="<TOMCAT_HOME>"
-Djava.io.tmpdir="<TOMCAT_HOME>\temp"
org.apache.catalina.startup.Bootstrap start
Note: I've replaced the real path by <TOMCAT_HOME> in the results from above.
TL;DR do this:
Open cmd
Execute wmic
Execute process
Wait few secs and search java.exe and the arguments containing Tomcat jars.
Since you explicitly mentioned *.bat and Windows here is your answer.
1.Most tomcat application comes as a zip instead as msi/exe . The place where you extract is actually its locations . You start the application by clicking startup.bat file
2.Open the bat file with notepad . You will also find the relative path of the application.
3.Look for the path variables .
Hope this information helps
I am trying to run a simple JAVA program once per day on a Windows 7 machine.
My code runs fine inside NetBeans. If I do a clean and build it suggests this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This does not work from the DOS prompt of course because of the space between program and files so I do this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This works from the DOS prompt.
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
with arguments:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
When I then run it, all I see is a DOS box flashing up for a second. I expect the code to take about 30 secs to run. The code should persist data to a database and no updates happen.
The code also uses java.util.logging so I should see log entries and I don't.
I strongly suspect that I am not running the JAVA command properly or that there's a bad classpath issue that it present when running via Scheduler that isn't there when running from the DOS prompt.
Help would be appreciated. If you've seen this before and can sort it that would be great. If you can tell me how to get a meaningful error trace from Scheduler than that would also be really helpful.
Thanks!
I Think that you could create a simple batch script that will launch your program in this way :
#echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
I solved it after changing all fonts' references to "SansSerif"
I was using Jasper Reports inside Java to create a PDF file. It was working fine when I double click the batch file or Scheduler with Windows Server 2003 but not working with the Scheduler of 2008.
I tried many different things nothing worked so I though Could it be that Windows Server 2008 is blocking the access?.
Now is working perfect. So, if you are having problems check the references to anything you are using.
The scheduler will run under a different user unless you specify what user to run as. If it isn't running as your user then it won't be able to write to your directories.
The real problem to the original question is a java installation issue on Microsoft systems. Java jre installs into Program Files\java. The executable (java.exe) is only installed in that java\bin directory. Running from the command line, the os looks in the proper location for the java.exe. Running from other MS tools (such as VBA Excel or in this case TaskScheduler), it does not!
You can see that TaskScheduler is looking in the wrong place by viewing the tasks history in the TaskScheduler tool. Double click on some of the history events and one will list the action and return code. The action will show that the TaskScheduler is trying to run
"C:\Windows\system32\java.EXE"
So, copy java.exe from the java\bin directory into the place where the scheduler is looking, and now it will work.
Or update your task and provide the full path to java.exe.
You can also update the environment system path to look for java in the java\bin directory, but that has to apply to all users and sometimes this is faulty as well.
In an application I am writing, I am launching another application (a runnable JAR) using Runtime.exec(...). Everything launches successfully in Windows, but Linux (specifically certain installations of CentOS - works in Ubuntu) has been giving me some problems. For some reason, the only way the secondary application will successfully launch is if I execute the first through a terminal. All behavior works as expected. However, if I launch the first application by double-clicking its icon (without a terminal open), the button to launch the second application seems to do nothing. I get no exceptions or error output - just a flash of my progress bar saying that it is launching, and then nothing. I can confirm through jconsole that the second application's process is never launched.
I have seen the commonly linked article on the pitfalls of the exec method ( http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ), but have not been able to solve this problem with anything I have found there. I am in fact reading the output and error streams of the second process, as I see all output when it successfully runs (after launching the first application through a terminal command). Not knowing a lot about deeper workings of Linux, I think this sounds like it may be a permissions issue with the output stream or something, but I am not sure.
In case it helps to diagnose the problem, I am using the command:
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."}); Since this works (depending on how the application is launched), I'm not too concerned that anything is wrong with this piece of code...
Anyone have any suggestions? Thanks in advance!
EDIT: The solution was to fix the directory to the JAR I was attempting to run. When launched via the GUI, user.dir was pointing to the parent directory of the folder containing my application. Since I'm using Eclipse RCP, my solution was to use
String currDirPath = Platform.getInstallLocation().getURL().toString(); instead. Thanks for the help everyone!
Since you're just using the jar file name - myjarfile.jar - and not the full path to it, depending on the current working directory, the jar may or may not be found. Try changing your exec command to use the full path to the jar instead. You can debug this by using rt.exec() to write the output of 'pwd' to a text file.
instead of
rt.exec(new String[]{"\bin\bash", "-c", "java -jar myjarfile.jar myArg1 myArg2 ..."});
use
rt.exec(new String[]{"\bin\bash", "-c", "/***path to java***/java -jar myjarfile.jar myArg1 myArg2 ..."});