Launching another java application fails from java web start - java

As, it is easy to download jars and other artifacts using
http://code.google.com/p/jnlpdownloader/
So, I am trying to execute my business application from a small launcher application(this launcher application is running as Java web start application).
This launcher application,
first download executable business application jar from URL(available at runtime only)
And then execute the executable business jar.
But, the following code doesn't seems to work, nor, it is throwing any exceptions
String[] command = {"java -jar", "JarFromURL.jar"};
Runtime r = Runtime.getRuntime();
try {
r.exec(command);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "Voilla...") ;
}

The array of parameters passed to exec() should be split this way.
String[] command = {"java", "-jar", "JarFromURL.jar"};

Related

Cannot execute external vb script from Java program through Jenkins Slave setup

Through Jenkins - Slave setup (running in Windows), we have created a ANT job which in internally calls the below JAVA Program,
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
Process p = Runtime.getRuntime().exec(command);
System.out.println("Process Completed");
The ReadEmail.vbs file never gets called or executed.
There is no error message or warning getting generated.
When I run this java program from eclipse or through Master Jenkinks, VB Scripts gets executed without any errors.
Your
String[] command = {"cmd" , "/c", System.getProperty("user.dir")+"/Read_email/ReadEmail.vbs"};
relies on the executing process to know where to find cmd.exe and who to call for a .vbs.
I used a 'fully redundant':
String[] command = {"C:/WINDOWS/system32/cmd.exe" , "/c", "C:/WINDOWS/system32/cscript.exe", "E:/trials/SoTrials/answers/21228622/java/callme.vbs"};
try {
Process p = Runtime.getRuntime().exec(command);
} catch(Exception e) {
System.out.format("%s\n", e.toString());
}
successfully from a simple commandline program. I hope this strategy works for your more complicated Jenkins setup.

Run jar from within a Java application

I am trying to build a simple auto updater for my application. I am currently checking the local application version against my remote version. If there is a newer version I want to start my updater.jar - which basically downloads and replaces the old application.
My problem is that I cannot seem to get the updater.jar to start if there is a new version.
The code I am currently using is:
Runtime runtime = Runtime.getRuntime();
try {
Process proc = runtime.exec("java -jar updater.jar");
} catch (IOException ex) {
Logger.getLogger(Splash.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
The application exits but updater.jar is never launched..
Any ideas?
Your child process is likely exiting when your parent process exits.
When you launch a process you should usually:
consume stdout/stderr from the child process. If you don't do this your child process can block waiting for its output to be consumed. You should consume in separate threads. See this answer for more details
use Process.waitFor() to capture the exit code from the child process
It looks to me like you want to spawn the updater, let it perform a download and then exit your parent process. Anything more complex would likely be platform dependent (e.g. spawning a background process and disowning it)
Maybe the path to updater.jar should be specified in the java -jar command.
You better use the URLClassLoader since jre1.2
see How to load a jar file at runtime

Starting a Java application at startup

I have a Java application.
The application has a setting that decides whether or not the application starts at startup.
Currently, I have it this by placing/removing a shortcut in the StartUp items folder.
However, I am wondering if there is a better way to handle this behaviour.
EDIT
Yes, it's Windows. Sorry for not clearing that before.
The application has an UI where the user may trigger actions, also the application runs a few tasks in the background periodically while running.
#Peter, how could I change the registry with code from within the application? Is that approach compatible with all versions of Windows?
Below is a small example snippet of how it can be done from inside your application
static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";
private void exec(String[] args) throws Exception
{
if (args.length != 2)
throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");
String key = args[0];
String value = args[1];
String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value });
Runtime.getRuntime().exec(cmdLine);
}
I'm pretty sure this will work with all versions of Windows since they all use the same Startup\Run registry entry.
Hope that helps! :)
Credit
On Windows I have used open source Java Service Wrapper to make our application as window service which you can setup automatic at startup.
What you need to do is to download latest wrapper.exe and create wrapper.config file put all the configuration like Main class any VM arument other parameters in defined standards and create a window service by this exe
Use the Registry to start your program at the startup and then it will be shown in the list provided by msconfig commnd through Run.
Use this registry path
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Java Command Process Chain Generator

I have build a Java command line application which consists of several moduls. So when you start the application via the command line, you have to pass one parameter and its options like this for example:
cmd-> java -jar application -startModul1 option1 folderPath
OR
cmd-> java -jar application -startModul5 500 folderPath 1222
Currently I have to start each modul by starting the application and passing the requested parameter+options. For now thats finde but later, when I have lets say 20 modules, I want to generate a proccess chain with several moduls started one after the other.
For example at the end I could start both modules from the example above with just one command.
cmd-> java -jar application -startAllModules option1 500 folderPath 1222
Is there a framework, where I can generate such a proccess chain with existing command line modules? This should not be NOTHING programatically because I want to have some sort of xml-file or whatever, where I just configure a process chain and where I can select the modules and its parameters that should be run with one command.
Have you thought of turning your program into an interpreter?
I think that parsing your command line, understanding what simple commands it must execute (from the xml you want to use) and launching them is enough.
How to launch them?
Process p = Runtime.exec(String[] cmdarray)
where cmdarray will have each of the words of the command:
{"java", "-jar", "application", "-startModul1", "option1", "folderPath"}
and
p.waitFor();
if you want this thread to wait until launched command ends.
Update: non concurrent
The later was in case you want to run several independent processes in parallel. One for command you need.
In case you only need to execute them one after the another there's a more simply way. When the main realizes it must execute multi modules, it calls itself with the appropiate arguments.
public static void main(String[] args) throws Exception {
// parse params
if (it's a multi module command line) {
for (each module you have to execute) {
main(new String[] {"-startModule1", ..., ...}); // call myself with different args
}
}
else {
// execute what you've been asked for
}
}

JSmooth generated exe does not show Splash Screen

I wrap my Java Swing application as an exe using Jsmooth but I can see no way to take advantage of Java 6 splash screen option. I have the following manifest file:
Manifest-Version: 1.0
SplashScreen-Image: resources/LOADLOGO.png
Main-Class: se.bookingapp.UI.MainFrame
The splash screen appears if I simply click on the jar file of the application. However, the JSmooth generated exe form of the jar file does not show the splash screen somehow. Does anyone know why?
Yesterday I've finished to develop my java application and I had the same issue. If I double click the .jar file or I execute in a command line splash screen works perfectly, but when I execute the wrapped file it doesn't. Seems just JSmooth doesn't support this feature.
However I made a little trick to have a wrapped .exe and splash screen working at the same time. I made a little application called ApplicationLoader.jar that consists in a single main class that execute java -jar "Application.jar" in a command line. Here is the complete code:
public class ApplicationLoader {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
/* First I check if the first parameter is not null and it's not an empty string */
if(args[0] != null && !args[0].trim().isEmpty()){
/* Then I use java.util.regex package to validate the parameter is a .jar file */
Pattern pattern = Pattern.compile(".*jar");
Matcher matcher = pattern.matcher(args[0]);
if(matcher.matches()){
/* Finally I define the command line like: java -jar "Application.jar" */
String command = "java -jar \"" + args[0] + "\"";
try {
Runtime r = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
Process p = pb.start();
p.waitFor();
} catch (IOException | InterruptedException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error executing: "+command, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "The argument is not a .jar file!!");
}
} else {
JOptionPane.showMessageDialog(null, "There's not a valid argument!");
}
}
}
I have this folder structure for my application:
MyApp
+-- bin
| +-- MyApp.jar
| +-- ApplicationLoader.jar
+-- MyApp.exe
So in JSmoot I changed the classpath to ApplicationLoader.jar and add the relative location to my application in Application Arguments section like this:
And that's it. I know this is not the best option but is a workaround.
However there's a little problem:
Since ApplicationLoader.jar calls a cmd.exe then the wrapped .exe and your application will execute in two different processes.
This implies that if you have to kill .exe process for some reason (unexpected crash or something), your java application still working as a java.exe process. So in that case you must kill MyApp.exe and java.exe processes. Actually if you just kill java.exe process then MyApp.exe process will finish execution by itself.
If you keep this in mind and you can live with that I think this option is quite simple and useful.
I hope this be helpful to anybody looking for a workaround to this issue.
Does it work when you execute the jar file? Open it with WinRar for example, and check if the manifest is into META-INF folder, and LOADLOGO.png is in the right folder too.
After doing that, it should work. It works for me. Nothing wrong in your manifest.

Categories

Resources