question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating new application (Java With Maven, Java Application) has main class built ignoring templates

See original GitHub issue

Apache NetBeans version

Apache NetBeans 13 release candidate

What happened

Creating a new Java with Maven, Java Application generates a Java file with a main. The file is generated ignoring the available template for Java Main Class.

Somewhat unrelated to this issue, clicking on the nbfs: links did not cause any noticeable activity.

How to reproduce

  1. File | New Project
  2. In popup, choose Java with Maven then Java Application
  3. Hit finish on next pop up
  4. Generated project has a MavenProject1.java file with a main

It has

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
 */

package edu.uab.mavenproject1;

/**
 *
 * @author name <email>
 */
public class Mavenproject1 {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

There is an existing template viewable using Tools | Templates , select Java, then Java Main Class, open in editor

/*
 * Author: ${user}
 * Assignment:  ${project.displayName} - ${course}
 *
 * Credits:  (if any for sections of code)
 */

<#if package?? && package != "">
package ${package};

</#if>
<#if superclass?? && superclass != "" && !superclass?starts_with("java.lang") && !superclass?matches(package + "\\.\\w+")>
import ${superclass};

</#if>
<#if interfaces?? && interfaces != "">
    <#list "${interfaces}"?split("\n") as interface>
        <#if interface?? && interface != "" && !interface?starts_with("java.lang") && !interface?matches(package + "\\.\\w+")>
import ${interface};
        </#if>
    </#list>

</#if>
/**
 *
 */
<#if superclass?? && superclass != "">
    <#assign extension = "${superclass}"[("${superclass}"?last_index_of(".") + 1)..]>
</#if>
<#if interfaces?? && interfaces != "">
    <#assign implementation = "">
    <#list "${interfaces}"?split("\n") as interface>
        <#assign implementation += "${interface}"[("${interface}"?last_index_of(".") + 1)..] + ", ">
    </#list>
    <#assign implementation = "${implementation}"?remove_ending(", ")>
</#if>
public class ${name}<#if extension?? && extension != ""> extends ${extension}</#if><#if implementation?? && implementation != ""> implements ${implementation}</#if> {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }

}

Did this work correctly in an earlier version?

Apache NetBeans 12.6

Operating System

MacOS 12.2.1 (Monterey) M1 Pro

JDK

JDK 17

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

Note: the previous 12.6 did not add a starting file as part of project completion.

Are you willing to submit a pull request?

No response

Code of Conduct

Yes

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:23 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
neilcsmith-netcommented, Nov 2, 2022

Thanks @mbien - immediate issue with links fixed. Bumping this to NB17 for discussion on whether project templates should ignore file templates or not.

1reaction
sdediccommented, Jul 22, 2022

OK; the second link is supposed to lead to the template the file was created from. An the file exists (on the nbfs) in fact:

                <folder name="JavaApp">
                    <folder name="src">
                        <folder name="main">
                            <folder name="java">
                                <folder name="${packagePath}">
                                    <file name="${mainClassName}.java" url="nbresloc:/org/netbeans/modules/maven/resources/App.java.template">
                                        <attr name="javax.script.ScriptEngine" stringvalue="freemarker"/>
                                        <attr name="template.openFile" boolvalue="true"/>
                                    </file>
                                </folder>
                            </folder>
                        </folder>
                    </folder>
                </folder>

So the templating engine (or the template) should be enhanced to properly URLencode special chars (which it does not do at the moment). The replaceable tokens are intentionally in the path to allow substitution from API call parameters.

The other issue is that the entire Java Project structure is not shown in Templates - this should be (somehow) fixed. “Somehow” means that the template UI (Tools | Templates dialog) isn’t actually prepared for structured templates, where the user could eventually add conent etc - so some tweak will be needed to present the Maven-main-class template.

The reason why the Wizard does not use the “main” template is because the template instantiation call would invoke another New From Template wizard – this is how those templates are registered at the moment, and that Wizard does not support being parametrized from outside and run headless. That might be fixed as well - but will be probably more tough.

I can take as a Friday project the first two items (URL encoding / link fix + Templates UI appearance). I cannot promise the last at the moment - although it would be cool to be able to reuse other templates in project structure template.


The reason behind this all was to allow to create Maven projects in a non-interactive manner, througj an API (File Templates API) and from a LSP client (that uses the programmatic access).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maven - How to create a Java project - Mkyong.com
1. Create a Project from Maven Template · 2. Maven Directory Layout · 3. POM file · 4. Update POM · 5. Write...
Read more >
How do I tell Spring Boot which main class to use for the ...
My project has more than one class with a main method. How do I tell the Spring Boot Maven plugin which of the...
Read more >
5 Creating Java Projects - Oracle Help Center
In addition to Ant, the IDE also supports Maven, an open source build management tool. ... Java Application - An empty Java SE...
Read more >
Apache Maven Tutorial - Vogella.com
Maven created a App.java class in the ./src/main/ folder, which is just a simple "Hello World" program. It also created an example test...
Read more >
Getting Started with the Bare-bones Java App Template
IntelliJ can open this project and work with it natively without requiring any special plugins. This is because IntelliJ supports Maven projects ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found