Eclipse : how we take arguments for main when run - java

In Java, for a normal main method :
public static void main(String[] args){
// code here
}
String[] args is used to take some parameters from command line. I can run this file from command prompt by :
javac filename.java
java filename -30
But, it takes more steps, and I must cd to this folder. (to long for each time). So, Does anyway to run this file with some arguments for main in Eclipse.
Thanks :)

In Eclipse you can set up a Run Configuration for the Java Application. Click on the green "play" button in the Launch toolbar (next to the bug icon which starts debugging).
Within that configuration, you can set the working directory and command line arguments - and even prompt the user for command line arguments when it's run, using arguments like ${string_prompt:Foo}.

Right click the class. Then go to Run as > Run configurations. Select the program on the left side. Then on the arguments tab you will see Program Arguments. Enter your program arguments in this textarea, if you would like to pass multiple arguments separate the arguments by spaces.
This site provides a good step by step tutorial with images: http://www.javaprogrammingforums.com/java-jdk-ide-tutorials/362-how-send-command-line-arguments-eclipse.html

Right click on program with main method---> Select Runtime Configuration--> There you will see a box for argument on right side.

It was weird that I had to pass each argument in a new line but after reading a bit it made sense to me(overlooked the fact that they(Arguments) are instances of String).

Related

Status Monitor in Eclipse?

I am pretty new at developing larger software and I am using Eclipse as IDE.
When my program is running, I want to have certain informations about the status in different classes (like values of certain objects). Up until now I was just printing it all out on the console with System.out.println(object.value);.
After a while the console became confusing with all the different values printed higgledy-piggledy. Now I am searching for a plugin or something, with which I can do something like
StatusMonitor monitorSize = new StatusMonitor();
StatusMonitor monitorHeight = new StatusMonitor();
monitorSize.print(object.size);
monitorHeight.print(object.height);
And then Eclipse has two different terminals/windows where the specific variables are printed.
Is there a possibility to achieve that?
YOU SHOULD DEBUG IT. FOR THIS ADD BREAKPOINTS IN THE CODE(To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively you can double-click on this position.) WHERE YOU WANT TO CHECK THE VALUES OF VARIABLES. WHEN YOU HAVE PUT THE BREAKPOINTS IN YOUR CODE THEN RIGHT CLICK ON THE CLASS WITH MAIN METHOD THEN SELECT --> DEBUG AS--> JAVA APPLICATION. THEN A DIALOG BOX WLL OPEN CLICK YES ON IT AND NOW YOUR CODE WILL BE OPEN IN DEBUG MODE. ON THE TOP PANEL .There will be options such as STEP INTO ETC. ALSO THERE WILL ARE FEW SHORTCUTS:
F5-->Executes the currently selected line and goes to the next line in your program. If the selected line is a method call the debugger steps into the associated code.
F6-->F6 steps over the call, i.e. it executes a method without stepping into it in the debugger.
F7-->F7 steps out to the caller of the currently executed method. This finishes the execution of the current method and returns to the caller of this method.
F8-->F8 tells the Eclipse debugger to resume the execution of the program code until is reaches the next breakpoint or watchpoint.

how to set a specific position though command line?

String cmd = "start calc.exe";
Process process = Runtime.getRuntime().exec(codeString);
I can call calculator out, but I wish to specify a accurate position like (200,300).
how can I rewrite my cmd String?
I know that java.awt.window can set a window or frame to the specific position.
Is there any method I can use to fill frame or window with my process?
There is no clean pure java solution because JDK does not provide API that can control non-java windows. So, if you want to can use JNI/JNA.
But I can suggest you a patch that will typically work.
Windows OS allows moving windows using keyboard. Try the following manually:
Win+R
type calc and press enter
press alt+space
press M
press enter
now use arrows to move the window. Press ESC to exit this mode.
All these actions can be implemented using java.awt.Robot.
So, you can run calculator and then immediately move its window where you want.
Well, this is not clear solution, but very simple one.
Expected Problems:
Alt+space is mapped to other, custom application
Other window that started together with calc overlaps it.
User will see that window is created somewhere and then quickly moved.
So, everything depends on how important all this for you. This solution is good as an exercise or demo but bad for real commercial application.

Conflict in a project due to presence of two green arrows

I am new to Netbeans and trying to piece together my application by using code from different sources. I have a query. In one of my projects there are two different files with green arrows against them - I think the green arrow indicates the presence of a main routine. I want to prioritise which file has the main routine and wanted to know the easiest way to do this. Would this simply be a case of removing the line public static void main(String args[]) in one of my projects - If not how do I do it. I'm using Netbeans 7.4
Every file with a main method will have the "green arrow". Normally, a project should only have one main method, the launching point of the program.
You can't change the green arrow in the context your speaking of, but you can change which class is the launching class (Main Class).
right click your project.
from the context menu select properties
from the dialog choose run on the left.
where it says Main Class select the class you want as the launching point class.

How can I check if an object has been created/called upon in Java? (Eclipse IDE)

Suppose I was making a game and within the render method which cycles hundreds of times per second I have two lines of code e.g.
Sprite sprite = new Sprite (...);
screen.renderSprite(...);
Which creates an object and renders it onto the screen so it shows. But suppose it didn't show; it would be natural to assume that if there is no error then it just wasn't called.
So in Eclipse how I can check if these two lines of code have been called in line x? If you tell me to use breakpoints tell me step by step because I feel that would only obfuscate the render method. (?) I want to check if the object has been created and utilized.
If you need more information don't hesitate to ask.
11. Sprite sprite = new Sprite (...);
12. screen.renderSprite(...);
Right click on line 12 (in my example but will be different in your code.)
Click on Toggle breakpoint
Click Run > Debug As an then either selct Java Application or an existing launch configuration
Once the code starts and enters screen.renderSprite, the program will halt and you'll be able to check it.
First of all you will have to identify some lines in your code at which you know that your sprite wasn't rendered (maybe a state flag?).
From that you have two options:
If you maintain the source code of method #renderSprite(...) you should consider using a logger (like apache logging which should be already included as a plugin of your Eclipse IDE). Thus your application wouldn't obfuscate your rendering processes.
The other option would be debugging: Set up a breakpoint (double-click left of said lines) and run your application in debug mode. You can also set conditions for the breakpoint to stop only in certain cases. Therefor you'll have to right-click a breakpoint and choose "Breakpoint Properties..." and check the "conditional"-box. In the textfield below you can define your condition in Java (you'll have access to all variables just like in your Editor). For more information see the debug help page of Eclipse or Lars Vogel's tutorial on debugging. By debugging your rendering would be paused, but it should be easier for you to find the concrete source of your problem.
A line with a breakpoint set, whose bytecode has been executed, will have a little checkmark on the breakpoint image.

Make java application ask for arguments before run

I want my Java application to ask for specific arguments (like name and email) before running in Eclipse. How can I make it do that?
It's not part of the Java code - it's part of the run configuration.
Go to the Run drop down (the down-arrow next to the run button), select Run Configurations, and then find your program and select it.
Go to the Arguments tab, and then in the Program Arguments click on "Variables..." to get a list of possible variables for Eclipse to use. For example:
${string_prompt:Foo}
That will prompt the user for a value for "Foo" when you then run that configuration.
Edit the run configuration of the class you want to run, go to the Arguments tab, click the Variables... button under the Program arguments text box, and select one of the *prompt variables. Do that for each argument.
While this approach would not technically ask the user for input before the java application starts running, it could accomplish basically the same thing:
You could use an easy-to-use SWING class, JOptionPane. This way, the input prompts would be a part of the application, but if you put them at the very beginning of the main() method, it have roughly the same effect as prompting before the application starts. Using this, you'd add something like the following to your source code, at the very beginning:
String name = JOptionPane.showInputDialog("Please enter name:");
String email = JOptionPane.showInoputDialog("Please enter email:");
Here is a reference to how the JOptionPane class works.
Using this, a popup box in which the user can enter text will appear when the application is started.

Categories