In NetBeans Java, how do you create a multi-module project? - netbeans

I want to create a multi-module Java project, in NetBeans 15.
By "module", I mean the Java-9 "modularity" feature, where each module folder has a module-info.java file that specifies its exports or depends or etc.
I know how to code a multi-module application. I just don't know how to do it as a NetBeans project. Yet I'm sure that lots of you StackOverflow readers have been there, done that. How can I do that, too?
Also, I am not using a plug-in, I am not using JavaScript. I am not using Maven, but Ant would be OK.
There is a complete tutorial at https://netbeans.apache.org/tutorials/nbm-projecttype.html, but this shows how to create a NetBeans module, for the NetBeans IDE, so that is the wrong place to look.

Got it! I've succeeded, I built a modular project in NetBeans.
But first, I had to understand how modules work in Java. I kept my coding as low-level as possible. I used only non-IDE file explorer, NotePad++, and Windows Command Prompt. Instead of automating the process with CMD scripts, I stuck with Java's own #argfiles. The strategy was KISS (Keep It Simple Stupid), even though the typing was brutal, I really really had to watch where and what I was typing. And plenty of times, I wished I had the NetBeans "Tools" and "Edit" features. But finally I am ready to turn to NetBeans.
Yes, NetBeans does modules!
Follow these steps:
New project > Java with Ant > Java Modular Project.
Project Name and Location is your choice.
In the "Projects" window, right-click the name,
New Module, and add the module name
In the default package, right-click, and
add your packages
add your classes
code your module-info
another New Module etc etc
eventually you'll get your modules in. NetBeans is good at catching typo's and missing items.
run your multi-module program!
All this procedure is almost as easy as writing a non-module program. The extra layer of modularity should be logical and functional. For my project, I had to think twice about module boundaries and names.
So! It's working. The NetBeans IDE and debugger are working for me.

Related

Define a Java 9 multi-moduled project in Eclipse

I'm trying out Java 9 Jigsaw module system (no module experience yet) and would like to use it for capsuling the classes within my project, but it's confusing.
According to this article it should be possible to have multiple modules within ONE project. I made a new project in Eclipse Oxygen (Java 9 is supported) with the same structure as shown in the article. But Eclipse keeps telling me that I must not have more than one module-info.java in a project.
I really don't know how to tell Eclipse that it should use the "multi-module-mode". And I really would appreciate not having to create a new project for every single module.
This works:
This not:
But according to this article something like that should work:
And how about deployment of a modularized project with Eclipse? There is nothing to see about the new jmod extension. Do I still export it as a runnable JAR file like before?
Notice that my questions refer to working with the IDE (no command line, I mean with an IDE that should be possible, right?) Thank you for enlightening me.
Currently, Eclipse requires you to create a separate project for each module (e. g. because each module has its own Java Build Path).
To understand this design decision, consider that Java modules correspond to OSGi bundles / Eclipse plug-ins and it has always been to have a separate project for each bundle/plug-in. If you come from the Maven world, you would probably expect a deeper folder structure instead. But modules are self-contained and combining several modules into one project would only add an additional folder level without meaning. However, Eclipse supports nested projects and so-called working sets if you need an additional folder level.
Exporting modules as images is planned for Eclipse 2019-03 (4.11), on March 20, 2019 (see Eclipse bug 518445). Exporting modules as JARs that can be used on the modulepath (-m) already works (see my video).
I don't know if this question is still open for an answer, but you can solve this problem by simply removing all source folders on the build path. At least this works for Eclipse 2021-12 version.
As you can see this is a demo project from the Official Gradle Guide Book and it has multiple modules. Each module has its own module-info.java.
project structure in IntelliJ IDEA
If I open this project in Eclipse it will give me the 'duplicated entries on module-info.java' error.
Eclipse shows the error
But if I delete all the source folders on the build path, the error is gone and the project can be built and run without problem.
project properties: Java build path
The only problem is that you have to build the project with Gradle so that it will produce the .jar of each module and you have to include them in the libraries later.
include all the .jar in libraries
I think this is probably the same solution mentioned by howlger above.

Using CDT without Eclipse

I want to use CDT parser in a project. The project would be a command-line, stand-alone project, i.e., not an Eclipse plugin.
All solutions that I've seen requires using a IWorkspace. But, I want to use the CDT parser on single files outside eclipse. Is there any way to do that?
It depends what you mean by "without Eclipse".
CDT's code is built on top of the Eclipse Platform, so you're going to need to be running an application that includes the Eclipse Platform. However, there's no reason that application can't be a command-line application. These are called "headless" applications in the Eclipse community, and you can find many tutorials for making one (here's one).
Requiring an IWorkspace shouldn't be a problem. You can e.g. create a workspace and a project in a temporary folder, and copy the code to be analyzed there.
If, for some reason, you really want to use just CDT's parser in an application that doesn't include the Eclipse Platform, you can probably copy the parser code from CDT, and replace any dependencies it has on the Eclipse Platform with your own implementations. However, this is likely to be a more labour-intensive approach, and I wouldn't recommend it.

Is it possible to build a Netbeans Matisse based project without Netbeans?

Part of a project I'm on has a GUI app that is built with Netbeans using the GUI design tools that come in that IDE.
But this is only part of a bigger project, and needs to be built in an environment that does not have Netbeans installed (it happens to be a Gradle-based build on Jenkins). I've gotten builds to (seemingly) work, but keep running into inexplicable run-time errors (i.e., dependencies appear to be met with identical jars on the classpath and so on, but attempts to read resources from the project jar fail.).
From googling around, it looks like this might have something to do with compiling .form files and then including some dependency for the resulting java. (though, the Netbeans build does not add any jars above what our gradle build adds).
So the question is... can this even be done? or does a proper build simply rely on some hidden build-time mojo that's going on in Netbeans?
Yes, you can compile the classes without NetBeans. Just be sure you do not any NetBeans library, like AbsoluteLayout. The .form files are for NetBeans showing you components with Matisse, but all the generated code will be in the .java file.

Writing an Eclipse-aware Ant task

I would like to write a simple Ant task that would interact with an Eclipse workspace to get some information from it. I would like to be able to use the various Eclipse API's (for example, IWorkspace).
My question is what would be the simplest way to go about doing this.
I have excellent knowledge of the Eclipse platform as a user of it - but none with development for Eclipse. I understand that, in runtime, my Ant task will have to be invoked under the same JRE as the workspace - that's a restriction I'm willing to be bound to.
I started by creating a Java project to hold my Ant task. I believe that the only thing in my way now is how to define the JAR dependency on Eclipse's JARs. I obviously don't want to depend on one specific Eclipse JAR as these are versioned (for example, Eclipse's "Resources" plugin). Is it possible to have my Java project buildtime-depend on "whatever the current JAR is for the Resources plugin"?
I think this article quite much addresses your problem: http://www.ibm.com/developerworks/rational/library/09/eclipsecustomanttasks/
It is targeted to the IBM Rational Application Developer, but that one is based on Eclipse and AFAIS there is not overly much non-eclipse-specific happening here, at least not regarding the specifics about how to make the task available in Eclipse (writing the plugin definition etc).
So, you want an Ant task that runs within Eclipse. And as you say, it's clear that those tasks are using the Eclipse API. As of this moment, I don't really understand how they're doing it. I've looked at the source of a couple of them and I still have questions.
To find the locations of all the Ant tasks contributed by Eclipse, do a Plug-in Search for org.eclipse.ant.core.antTasks. When I do that, I see twenty or so extensions, many of which define multiple tasks.
If you get the source bundle for a plugin that contributes one of these tasks, you can look at the source for it. RefreshLocalTask is in org.eclipse.core.resources; unfortunately, when I import this bundle into my workspace as a source project, the source for the Ant tasks doesn't get linked correctly. There is a separate jar (inside the bundle) for them, and, while the source is in the bundle, it's not clear how the jar is compiled. The upshot is that I don't have the Ant task source compiling in my workspace.
You can also Google for the Ant task source; here's the 3.6.0 source for RefreshLocalTask.
Anyway, in the source you can see calls to org.eclipse.core.resources.ResourcePlugin that are illustrative of what you probably need.
Ah, I see in the extension point description - right-click on one of those search results and choose "Show Description", or go there from the Manifest editor - that there is a flag you can set, "eclipseRuntime"; the text implies that if it is set, Eclipse will launch the task in the same VM.
You should probably say more about what you want to do, because there are several routes you can take.
Eclipse provides some Ant tasks that you can use in your build scripts. Here are a few. There are more, so search for "ant tasks" in the Eclipse docs; they're scattered throughout different pages. Eclipse Preferences (Window..Preferences, then select Ant/Runtime, and look at the "Contributed Entries" in the Classpath tab) shows you a list of Ant tasks that Eclipse contributes to the runtime whenever Ant is invoked from Eclipse; you can invoke any of these tasks yourself as long as you include the right jar.
It's possible to start up Eclipse from within an Ant task, because it's possible to start Eclipse from Java. You need to include the right jars and make the right calls; you also need to configure Eclipse correctly. "org.eclipse.core.runtime.adaptor.EclipseStarter" should give you some detail.
Perhaps you can use one or more of the variables Eclipse defines for use when launching Ant (or any other program). Try creating an External Tool Configuration (at the bottom of the Run menu) - select an Ant Build and try customizing it with arguments (from the Main tab) or environment variables (from the Environment) tab. Both give you access to Eclipse variables. But of course these are just values you can pass into your Ant script, not handles to anything you can invoke a method on.
What you can't do: I'm pretty sure that when Eclipse launches Ant, it is always in a separate VM, with no way to call back into Eclipse.
So perhaps you should say more about what you want to do.

How to compile & run a project-less file in Netbeans?

Netbeans, indeed any Java compiler and
executor, needs to know the classpath
to compile and run a file. When you
just open a file in Netbeans, you do
not tell it any of that information.
When you set up a project, that
process tells Netbeans where the Java
resources are.
Yet other IDEs can easily do this. It's not good practice, but it's simple and quick for "fooling around".
Is there any way to compile & run a file, that doesn't belong to a project, in Netbeans?
edit: The options are greyed out if the file doesn't belong to a project.
Your best option with NetBeans is just to create a dummy "try stuff" project that you know will never contain a shipping product but will allow you to experiment without having to create a new project every time.
You could even collect code snippets in different classes this way. If you add a main() method to each experimental class, you have a way to run them all in the IDE just by changing what you have set for the main project class. This is what I do and it works pretty well.
Why not using 'create new project with existing sources' which can be done for Java, Ruby, ... projects?
You can do so and netbeans will create only a nbproject folder within your project's dir and all should be fine.
This solution doesn't work great because projects are also "typed" and I work in 4 languages. I'd hate to have a "rubystuff" project, a "pythonstuff" project, "javastuff", etc, etc...