Netbeans How to Set Command Line Arguments in Java

Netbeans how to set command line arguments in Java

I am guessing that you are running the file using Run | Run File (or shift-F6) rather than Run | Run Main Project. The NetBeans 7.1 help file (F1 is your friend!) states for the Arguments parameter:

Add arguments to pass to the main class during application execution.
Note that arguments cannot be passed to individual files.

I verified this with a little snippet of code:

public class Junk
{
public static void main(String[] args)
{
for (String s : args)
System.out.println("arg -> " + s);
}
}

I set Run -> Arguments to x y z. When I ran the file by itself I got no output. When I ran the project the output was:

arg -> x
arg -> y
arg -> z

NetBeans IDE, set command line arguments and run as main project, however, nothing is showing

You have to use the big green arrow to run your project, or right-click on the project itself and select "Run.." from the menu.

If you use Shift-F6 ("Run File") it won't supply the command line arguments. Those are for the project only.

Command line parameters specified in NetBeans 8.2 turns up when running .jar from command line

You could add a configuration (Project->Properties->Configurations->Add), select the created configuration under "Project->Properties->Run" and specify the arguments only for this configuration.
But this way is NetBeans specific. If you are using Maven you should use profiles. Add something like this into your pom.xml:

<project>
...
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!--your command line arguments-->
<exec.args>hello world</exec.args>
</properties>
</profile>
</profiles>
...
</project>

You still would need to remember to deactivate your development profile before shipping but like this you can separate the configuration between development and production system in a clean way. (Especially if later on there should be other configuration values which should differ, like logging level, different paths and so on).

Trouble Using Java Command-Line Arguments in NetBeans

I am unable to reproduce your issue in my environment, however the most likely cause is that:

Your main class is WordCloudGenerator, and from what I can tell the code in the image you have shown is not from WordCloudGenerator class.

You need to make sure that your main class is set correctly in the properties window shown in the first image in your question. Or if you do want two classes with main methods, then WordCloudGenerator needs to forward those args onto your other class like so:

public class WordCloudGenerator
{

public static void main(String[] args)
{
myOtherClass.main(args);
}
}

Now your main method in the other class should function correctly.



Related Topics



Leave a reply



Submit