X

Geertjan's Blog

  • April 23, 2011

Running the Generated Installer from the IDE

Geertjan Wielenga
Product Manager
When you're generating installers for your NetBeans Platform application in the IDE, it helps to be able to run the generated installer right inside the IDE. I.e., I want to be able to right-click the generated installer and choose "Run" and then the installer starts. However, the IDE does not recognize my installer file type (which, since I am on Windows, is an ".exe" file).

So here's how to run ".exe" files from within the IDE (or any other NetBeans Platform application). First, use the New File Type wizard to recognize files with the MIME type "application/exe", with "exe EXE" as their possible file extensions.

Next, create a "RunAction", which you bind to the MIME type via annotations (which will result in the required layer file entries being generated at compile-time):

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.ExecutionDescriptor.LineConvertorFactory;
import org.netbeans.api.extexecution.ExecutionService;
import org.netbeans.api.extexecution.print.ConvertedLine;
import org.netbeans.api.extexecution.print.LineConvertor;
import org.openide.loaders.DataObject;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileUtil;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
@ActionID(category = "Build",
id = "WinExeSupport.RunAction")
@ActionRegistration(asynchronous = true, displayName = "#CTL_RunAction")
@ActionReferences({
@ActionReference(path = "Loaders/application/exe/Actions", position = 0)
})
@Messages("CTL_RunAction=Run")
public final class RunAction implements ActionListener {
private final DataObject context;
public RunAction(DataObject context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
File exeFile = FileUtil.toFile(context.getPrimaryFile());
String path = exeFile.getAbsolutePath();
execute(path);
}
public Integer execute(final String path) {
Integer get = null;
Callable<Process> processCallable = new Callable<Process>() {
@Override
public Process call() throws IOException {
return new ProcessBuilder(path).start();
}
};
ExecutionDescriptor descriptor = new ExecutionDescriptor().
showProgress(true).
frontWindow(true).
controllable(true);
descriptor = descriptor.outConvertorFactory(new LineConvertorFactory() {
@Override
public LineConvertor newLineConvertor() {
return new Numbered();
}
});
ExecutionService service = ExecutionService.newService(processCallable, descriptor, path);
Future<Integer> task = service.run();
try {
get = task.get();
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
return get;
}
private static class Numbered implements LineConvertor {
private int number;
@Override
public List<ConvertedLine> convert(String line) {
List<ConvertedLine> result = Collections.singletonList(
ConvertedLine.forText(number + ": " + line, null));
number++;
return result;
}
}
}

Most of the code above is taken directly from this blog entry by NetBeans engineer Petr Hejl, about the NetBeans External Execution Support that Petr created in 2009. The nice thing is that threading is built into the above code, while the progress bar automatically starts, with the output automatically displayed in the Output window (with numbered lines, thanks to the LineConverter above).

Now I can run the generated installer from the IDE, which is very convenient because now I do not need to switch to outside the IDE when trying out my generated installer. Of course, all other ".exe" files can now also be run from the IDE.


Join the discussion

Comments ( 2 )
  • Herv&eacute; Monday, April 25, 2011

    Hi Geertjan,

    I cannot figure out what you mean by "New File Type wizard"

    I've tried the Tools|Options|Miscellaneous|Files dialog where I can specify an extension such as "exe EXE" but the MIME type has to be chosen from a predefined list. And no application/exe is available in the list.

    What am I missing?

    Thanks

    /Hervé


  • Geertjan Wielenga Monday, April 25, 2011

    Herve, you need to create a new module. Then, in that new module, use the "New File Type" wizard. Or just follow the tutorial here:

    http://platform.netbeans.org/tutorials/nbm-filetype.html


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.