NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Passing in command line arguments while using "Run File" or "Test File"

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
IceMan81



Joined: 02 Jan 2009
Posts: 17

PostPosted: Tue Oct 13, 2009 2:53 pm    Post subject: Passing in command line arguments while using "Run File" or "Test File" Reply with quote

Hi,

Could someone tell me how to pass in command line arguments when you want to Run a file having a main() method?

The only way I am able to pass in command line arguments is by setting the main class of the project to be the class I want to run (and contains a main () method) and then running the project. When you do this Netbeans picks up the arguments under Run in project properties.

This looks like a convoluted way to do this. Please let me know if there is a better way to do this.

Thanks
Back to top
xiuh



Joined: 07 Jul 2009
Posts: 6

PostPosted: Tue Oct 13, 2009 3:07 pm    Post subject: passing args Reply with quote

Hi, maybe I misunderstood what you were asking, but there is a 'run' dialog box in project properties. In this tab you'll see an argument dialog box. Errr well that's what I use, to pass my args, if I've over simplified things you'll need to clarify a bit more.

woops, missed part of your post, guess this isn't what you were after, sorry I can't answer that question.
Back to top
Gregg Wonderly
Posted via mailing list.





PostPosted: Tue Oct 13, 2009 4:34 pm    Post subject: Passing in command line arguments while using "Run File" or "Test File" Reply with quote

IceMan81 wrote:
Quote:
This looks like a convoluted way to do this. Please let me know if there is a better way to do this.

Currently, the "run-single" action in the netbeans provided XML doesn't pass in
the command line arguments from the project properties. Only "run" does, as
you've found. There is no other mechanism that exists today. If you customize
the XML that netbeans generates, it will prompt you before it overwrites it.
You can construct and override of that target in your build.xml to do something
different.

Gregg Wonderly
Back to top
Thomas Wolf
Posted via mailing list.





PostPosted: Tue Oct 13, 2009 4:36 pm    Post subject: Passing in command line arguments while using "Run File" or "Test File" Reply with quote

On 10/13/2009 11:07 AM, xiuh wrote:
Quote:
Hi, maybe I misunderstood what you were asking, but there is a 'run' dialog box in project properties. In this tab you'll see an argument dialog box. Errr well that's what I use, to pass my args, if I've over simplified things you'll need to clarify a bit more.


The OP was pretty clear to me - he's not talking about running the
project (which employs the dialog you're talking about). You can run
any java file within a project that has a "public static void
main(String args[])" in it by selecting "Run File" from the right-click
context menu (or viat the Run->RunFile" toolbar entry). In answer to
the OP's question: I don't know of any way either. If there's no
existing RFE for this, please create one and I'll definitely vote for it.

tom
**********************************************************************************************
IMPORTANT: The contents of this email and any attachments are confidential. They are intended for the
named recipient(s) only.
If you have received this email in error, please notify the system manager or the sender immediately and do
not disclose the contents to anyone or make copies thereof.
*** eSafe scanned this email for viruses, vandals, and malicious content. ***
**********************************************************************************************
Back to top
IceMan81



Joined: 02 Jan 2009
Posts: 17

PostPosted: Wed Oct 14, 2009 12:46 am    Post subject: Re: Passing in command line arguments while using "Run File" or "Test File" Reply with quote

Yes, you can run any java file that contains a "public static void main(String args[])" by selecting "Run File" but it will never read in command line arguments. Only if you make the file the main class of your project will it accept command line arguments.

Gregg, your solution would most probably work but doesn't it seem obvious that if a file with a main() method can be run, it should accept command line parameters?

I will file and RFE for this and wait for a response from the Netbeans team.

Thanks for all the responses.
Back to top
Gregg Wonderly
Posted via mailing list.





PostPosted: Wed Oct 14, 2009 4:01 pm    Post subject: Passing in command line arguments while using "Run File" or "Test File" Reply with quote

IceMan81 wrote:
Quote:
Yes, you can run any java file that contains a
"public static void main(String args[])" by selecting "Run File" but it will
never read in command line arguments. Only if you make the file the main
class of your project will it accept command line arguments.



Gregg, your solution would most probably work but doesn't it seem obvious
that if a file with a main() method can be run, it should accept command
line parameters?

I will file and RFE for this and wait for a response from the Netbeans team.

I am on your side. I have no understanding of why run-file would not want to
pass all JVM args and all parameters to the class being ran. I use this for
mocking and testing all the time where I construct a simpler main than the
application uses to run something with just bits and pieces to test something.

Apparently, too many people have written test classes that do destructive things
and had the command line arguments cause them to have problems or something.

I'll vote for your issue!

Gregg Wonderly
Back to top
Superole



Joined: 18 Jan 2012
Posts: 1
Location: Trondheim, Norway

PostPosted: Wed Jan 18, 2012 9:49 am    Post subject: Reply with quote

I just managed to specify cmdline arguments pr class in NB7.0.1 using the following approach(I don't know where the magic happens, so bare with me):
1. I use maven (but again, I don't know if thats relevant).
2. I open the "Project Properties" dialog (right-click project -> Set Configuration -> Customize...).
3. I choose "Actions" in the Categories-tree (not "Run").
4. From the list of Actions I choose "Run file via main()".
I then get these values:
Execute Goals: process-classes org.codehaus.mojo:exec-maven-plugin:1.2:exec
Set Properties:
exec.classpathScope=${classPathScope}
exec.args=-classpath %classpath ${packageClassName}
exec.executable=java

5. Now append to the exec.args line: ${cl.args.${className}}
6. Add a new line with the cmdline arguments for a class with main():
cl.args.MyClass=-One -Two -Three
My "Set Properties:" field now looks like this:
exec.classpathScope=${classPathScope}
exec.args=-classpath %classpath ${packageClassName} ${cl.args.${className}}
cl.args.MyClass=-One -Two -Three
exec.executable=java

7. click OK
8. right-click MyClass.java -> Run File
9. observe that args={"-One", "-Two", "-Three"}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo