View Full Version : Help with arguments in Java
UltimateSomni
Jun13-11, 07:08 PM
I'm suppose to type this in and get a certain error and I did:
http://i56.tinypic.com/2qdyebn.jpg
So the writer says I need to type in three arguments, and his sense of humor brings me to do this:
http://i51.tinypic.com/2i1z138.jpg
I click OK
Then when I click Run again I again get the same error:
http://i52.tinypic.com/1rcbjk.jpg
What did I do wrong?
schip666!
Jun14-11, 11:29 AM
I'd say your IDE is screwing with your mind... It looks like you have enter arguments correctly, are you sure you are running that configuration?
Can you try running the class file from a command line?
java BlankFiller arg0 arg1 arg2
UltimateSomni
Jun14-11, 01:44 PM
I'd say your IDE is screwing with your mind... It looks like you have enter arguments correctly, are you sure you are running that configuration?
Can you try running the class file from a command line?
java BlankFiller arg0 arg1 arg2
http://i52.tinypic.com/20rttec.jpg
http://i52.tinypic.com/20rttec.jpg
You have to run that from the directory where the compiled BlankFiller.class file is located. If it didn't compile, there will only be a BlankFiller.java file.
UltimateSomni
Jun14-11, 04:11 PM
You have to run that from the directory where the compiled BlankFiller.class file is located. If it didn't compile, there will only be a BlankFiller.java file.
What?
Does anyone know why this isn't working? Did I input the arguments wrong?
What?
Does anyone know why this isn't working? Did I input the arguments wrong?
When you compile a java file, a .class file is created. That is what actually gets used when you run a java program. NetBeans probably created it but you have to be in that directory to run schip666!'s command line test. You ran your test from c:/users/fred. If BlankFiller.class isn't there, the test will fail with the reason shown on your link.
schip666!
Jun14-11, 05:02 PM
Time to learn about CLASSPATH...
Look for BlankFiller.class.
In your command window, cd to that directory.
Then try: java -classpath . BlankFiller 1 2 3
UltimateSomni
Jun14-11, 05:05 PM
I found BlankFiller.class
what does "cd to that directory" mean?
schip666!
Jun14-11, 05:08 PM
type: cd that-directory
god I love the GUI universe.
most likely there's something else wrong, but it is handy to know your way around a command line interface if you plan to program for anything but the web.
UltimateSomni
Jun14-11, 05:14 PM
ugh I have to leave but still no idea what you're trying to say
ugh I have to leave but still no idea what you're trying to say
cd is the dos command for "change directory". Let's say that your file is in a directory C:\Users\Fred\NetBeans directory (substitute what you found). Before you run the java command that failed, type this:
cd C:\Users\Fred\NetBeans
Note that dos is a really old languange and doesn't know about spaces. So if your directory had spaces anywhere, you have to put quotes around it like this:
cd "C:\Users\Fred\NetBeans"
Once you change to the correct directory, you can run your java test on the class file.
UltimateSomni
Jun14-11, 08:01 PM
http://i51.tinypic.com/316qgyc.jpg
Well, maybe that's some progress.
http://i51.tinypic.com/316qgyc.jpg
Well, maybe that's some progress.
My office blocks lots of web sites and the tinypic site that you're using is one of them. I can't help until late this afternoon unless you explain what happened.
UltimateSomni
Jun15-11, 09:07 AM
Command promt says the "1 2 dog jumped over the 3 fox"
And "the arg1 arg2 dog jumped over the 3 dog"
Command promt says the "1 2 dog jumped over the 3 fox"
And "the arg1 arg2 dog jumped over the 3 dog"
Could you also paste the content of your main method in code tags?
Also, what did you type into the command prompt?
UltimateSomni
Jun15-11, 12:26 PM
Can you see Google Docs? http://tinyurl.com/OrangeYellowArguments
And I did this exactly how the book told me to type it in:
class BlankFiller {
public static void main(String[] args) {
System.out.println("The " +args[0]
+ " " + args[1] + " fox "
+ "jumped over the "
+ args[2] + " dog."
);
}
}
schip666!
Jun15-11, 12:27 PM
Command promt says the "1 2 dog jumped over the 3 fox"
And "the arg1 arg2 dog jumped over the 3 dog"
That appears to be what you should expect to get. The argN's are put into the places that had arg[n] in the your original program code (IIRC...). Thus your netBums IDE is screwing with you somehow.
So... first, you may not be using the IDE correctly, but from casual examination of the screen shots you posted it appears that you are doing things right; so.... second, NET Beans is a networked java environment where programs are unlikely to have access to command line arguments anyway, thus they may have never implemented it right to start with.
UltimateSomni
Jun15-11, 12:31 PM
That appears to be what you should expect to get. The argN's are put into the places that had arg[n] in the your original program code (IIRC...). Thus your netBums IDE is screwing with you somehow.
So... first, you may not be using the IDE correctly, but from casual examination of the screen shots you posted it appears that you are doing things right; so.... second, NET Beans is a networked java environment where programs are unlikely to have access to command line arguments anyway, thus they may have never implemented it right to start with.
So my best bet is that this isn't my fault and I should wait for an update that fixes this issue?
The author is using Netbeans 6.5, I am using 7 anyway.
Try typing this in your command line:
java BlankFiller quick brown lazy
What comes back?
UltimateSomni
Jun15-11, 01:03 PM
Try typing this in your command line:
java BlankFiller quick brown lazy
What comes back?
https://docs.google.com/drawings/d/1ffRd24ddIKQtGcr6DcxKFRdcS98Z_NilFnSaeSC4pE0/edit?hl=en_US
That one I get what I should get, but I can't get the arguments I type in to work
Then it seems that the NetBeans IDE is not passing the command-line arguments to your program correctly, but if you run you program from the command-line, things are as they should be.
UltimateSomni
Jun15-11, 01:12 PM
So you recommend using an different IDE?
God damn you have tons of posts
So you recommend using an different IDE?Not if that's the only problem.
Is there a readme file for this IDE? That's where they usually list bugs that are known issues.
So you recommend using an different IDE?
The IDE probably isn't set up right. From the other screenshots that you posted, it is working properly in dos.
Unfortunately, I haven't used NetBeans to figure out why it doesn't recognize the variables. From what I read on the NetBeans site for Setting JVM Arguments (http://netbeans.org/kb/docs/java/project-setup.html#deploy-jvm), it looks like you've put the variables in the correct place. Could you look at that entire section for Running Applications (http://netbeans.org/kb/docs/java/project-setup.html#deploy) and verify that you've done everything the way that they've instructed?
UltimateSomni
Jun15-11, 05:41 PM
Am I suppose to type something into the VM Options box?
So I'm suppose to type in the fully-qualified name in the Main Class field (for example, org.myCompany.myLib.MyLibClass). How do I find that?
UltimateSomni
Jun15-11, 05:44 PM
How would I find a readme file?
How would I find a readme file?
Readme files are usually located in the top level directory where the program was installed. If you're using XP, it's probably somewhere in the Program Files directory.
Am I suppose to type something into the VM Options box?
So I'm suppose to type in the fully-qualified name in the Main Class field (for example, org.myCompany.myLib.MyLibClass). How do I find that?
I'm sorry but, I really am not familiar with how to set up NetBeans. Unless someone else here knows, your best bet might be to ask configuration questions on the NetBeans Forum (http://forums.netbeans.org/netbeans-users.html).
UltimateSomni
Jun16-11, 03:18 PM
So I asked on Netbeans, but the message board looks slow
Is there another free IDE you guys recommend?
So I asked on Netbeans, but the message board looks slow
Is there another free IDE you guys recommend?
I use IntelliJ and know that one very well. The current pay version is 10.5 but, I think that version 8 is free.
I wouldn't assume it's Netbeans and not something you did (or didn't do). I would be pretty surprised if they've managed to break such simple and basic functionality. You can try Eclipse if you like. I tend to use it for Java and Python. But Netbeans is quite good.
While I'm thinking of it, why don't you just copy/paste messages and such into the forums rather than making us look at screenshots and such? Because I'll tell you that I didn't look at them. If you want me to look at them, paste them into the forum. Taking screenshots of text is, in most cases, really pointless.
Anyways, I took some time out to make a quick test project in my Netbeans 6.9.1 (good to know there's a 7 out, I'll try that out later). I wrote out all my steps one-by-one. It works fine for me. Here's the steps:
1. Start Netbeans
2. In the menu bar, select File->New Project.
Make sure these are selected in the dialog box:
Categories: Java
Projects: Java Application
Hit Next
Fill in the Project Name field with: ArgTest
Take note of where it is putting your project in the Project Location and
Project Folder fields.
Hit Finish
3. Edit the Main.java file and add this to the main function's body (you can remove the comment in there, of course):
for (String arg : args) {
System.out.println(arg);
}
4. Click "Build Main Project" icon in toolbar (a hammer). Wait for it to finish.
5. In Run menu, select "Set Project Configuration->Customize...".
In the Categories sidebar, "Run" should be selected. In the "Arguments"
text edit, enter test arguments such as (without the quotes):
"arg1 arg2 arg3 arg4"
Click Ok.
6. Click the "Run Main Project" icon in the toolbar (green arrow facing
right). In the output pane, you should see the arguments you passed in.
I see this:
run:
arg1
arg2
arg3
arg4
BUILD SUCCESSFUL (total time: 1 second)
If you get a different result here, I'm rather surprised.
I'll probably install the latest version of NetBeans soon, so I'll check it out again in version 7 later.
EDIT: I installed NetBeans 7 (in Linux) and ran the project I created, and the arguments came through perfectly. I can try remaking the project from scratch in 7.0 if you like. But argument passing is not broken in NetBeans 7.0, at least in general. Could be it's a Windows only bug, but I really, seriously doubt it.
I've got one possibility why it might not see your arguments.
In the project directory, there's a subdirectory called nbproject. It has a subdirectory called private. In that directory, there's the file that should contain your arguments called private.properties.
My private.properties contains this line right at the start:
application.args=arg1 arg2 arg3 arg4
Make sure you have something like that in yours.
If it's not picking it up, it may have been overridden by something in your build.xml file (which is right in the root of your project's directory). Make sure you don't have a line like this:
<target name="-init-private" depends="-pre-init">
From what Some Guy On The Internet(tm) says, that causes the one in private.properties to be ignored. I don't have anything but the opening XML tag, a project element that wraps everything, and a description and an import tag inside.
Not 100% sure how to fix it (if that's it), but he says you need to put a line in your project.properties file (which is in the nbproject subdirectory).
I would try two things (keeping a backup of any file modified so you can put it back in place if anything breaks)). First, removing that offending line from the build.xml, if present. I would try that first. Not sure how it gets in there, or why, but perhaps removing it will fix it.
Otherwise, perhaps adding the args parameter that's in private.properties to project.properties will fix it.
So, in my case (with the example in the previous post), I would try adding this line from private.properties to project.properties:
application.args=arg1 arg2 arg3 arg4
Let me know if any of that works. Also, I'll repeat because it's important. Make backups of any file you edit so you can copy it back in place if it messes things up. This is just info from some random person on the 'net that I found. I haven't tried it out (for one, I don't have the problem you're having).
UltimateSomni
Jun16-11, 05:47 PM
Yes, so I click Run Main Project instead of Run File. It all works now.
I wouldn't assume it's Netbeans and not something you did (or didn't do). I would be pretty surprised if they've managed to break such simple and basic functionality. You can try Eclipse if you like. I tend to use it for Java and Python. But Netbeans is quite good.
While I'm thinking of it, why don't you just copy/paste messages and such into the forums rather than making us look at screenshots and such? Because I'll tell you that I didn't look at them. If you want me to look at them, paste them into the forum. Taking screenshots of text is, in most cases, really pointless.
Anyways, I took some time out to make a quick test project in my Netbeans 6.9.1 (good to know there's a 7 out, I'll try that out later). I wrote out all my steps one-by-one. It works fine for me. Here's the steps:
1. Start Netbeans
2. In the menu bar, select File->New Project.
Make sure these are selected in the dialog box:
Categories: Java
Projects: Java Application
Hit Next
Fill in the Project Name field with: ArgTest
Take note of where it is putting your project in the Project Location and
Project Folder fields.
Hit Finish
3. Edit the Main.java file and add this to the main function's body (you can remove the comment in there, of course):
for (String arg : args) {
System.out.println(arg);
}
4. Click "Build Main Project" icon in toolbar (a hammer). Wait for it to finish.
5. In Run menu, select "Set Project Configuration->Customize...".
In the Categories sidebar, "Run" should be selected. In the "Arguments"
text edit, enter test arguments such as (without the quotes):
"arg1 arg2 arg3 arg4"
Click Ok.
6. Click the "Run Main Project" icon in the toolbar (green arrow facing
right). In the output pane, you should see the arguments you passed in.
I see this:
run:
arg1
arg2
arg3
arg4
BUILD SUCCESSFUL (total time: 1 second)
If you get a different result here, I'm rather surprised.
I'll probably install the latest version of NetBeans soon, so I'll check it out again in version 7 later.
EDIT: I installed NetBeans 7 (in Linux) and ran the project I created, and the arguments came through perfectly. I can try remaking the project from scratch in 7.0 if you like. But argument passing is not broken in NetBeans 7.0, at least in general. Could be it's a Windows only bug, but I really, seriously doubt it.
Very nice!!
Yes, so I click Run Main Project instead of Run File. It all works now.
Excellent! Now it makes sense why it wasn't working.
Very nice!!
Thanks, Mark!
Yes, so I click Run Main Project instead of Run File. It all works now.
Hooray! :smile:
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.