X

Geertjan's Blog

  • December 2, 2010

How to Open a Project from the Command Line

Geertjan Wielenga
Product Manager
Maybe you'd like your users to open your NetBeans-based projects from the command line. If so, here's how:
@ServiceProvider(service = OptionProcessor.class)
public class OpenProjectOptionProcessor extends OptionProcessor {
private static Option project = Option.requiredArgument(Option.NO_SHORT_NAME, "project");
@Override
public Set<org.netbeans.spi.sendopts.Option> getOptions() {
return Collections.singleton(project);
}
@Override
protected void process(Env env, Map<Option, String[]> values) throws CommandException {
String[] args = (String[]) values.get(project);
if (args.length > 0) {
File projectFolder = new File(args[0]);//Perform some checks to make sure that the above
//is actually pointing to a folder that is a project,
//before continuining with the code below:

FileObject projectFo = FileUtil.toFileObject(projectFolder);
Project projectToBeOpened;
try {
projectToBeOpened = ProjectManager.getDefault().findProject(projectFo);//The "true" below is for opening the subprojects of the project,
//if that concept doesn't apply to your project type, return "false":

OpenProjects.getDefault().open(new Project[]{projectToBeOpened}, true);
} catch (IOException ex) {
} catch (IllegalArgumentException ex) {
}
}
}
}

Now your user can do this on the command line:

myapp --project "/home/foo/bar/myproject"

Related blog entry:

http://kgmodi.blogspot.com/2008/05/open-projects-programmatically-in.html

Update. I discovered the "--open" argument, which should be predefined in applications on the NetBeans Platform, at least in the case of NetBeans IDE, which can pass in either a file or a project, so "netbeans --open "/path/to/project" should work, saving you the trouble of creating the above command line processor in this case.

Join the discussion

Comments ( 5 )
  • Jesse Glick Thursday, December 2, 2010

    It is defined in o.n.m.utilities, in the ide cluster. BTW the --open can even be omitted (since this handler is marked as the default for unconsumed args); just 'netbeans /path/to/project' works, and you can pass multiple projects too.


  • ranjan Thursday, December 2, 2010

    Is there a way to execute commands by which netbeans can do code generation, for example generation of rest from database through command prompt.


  • Andy Thursday, December 2, 2010

    Question, when you want to know if there is a function that you could use from NBP, what are you going to do? scan the API list? This is too time consuming, and there is no thorough tutorials telling each and every API that is doing what, if you say so, then I have a question for you: which API FileUtil it belongs to? You used FileUtil, it's in the API but it's not in any API tutorial, so how did you find that?

    BTW, thank you for all the tutorial contributions!! They are amazing!!


  • Geertjan Wielenga Thursday, December 2, 2010

    "which API FileUtil it belongs to?"

    In your module project in the IDE, right-click "Libraries" and then use "Add Dependency" to get to the dialog where you can type in the class you're interested in and you will see the API you need.

    Also, you seem to not be aware of the FAQ:

    http://wiki.netbeans.org/NetBeansDeveloperFAQ


  • Geertjan Wielenga Thursday, December 2, 2010

    @ranjan: No. But you could use the code above to create something like that yourself.


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

Integrated Cloud Applications & Platform Services