Make java application ask for arguments before run - java

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.

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 do I rename all Scanners or object?

e.g., I have named my Scanner object readsir" throughout my code.
[Scanner readsir= new Scanner(new File("word.txt"));]
However, I am worried my professor would expect a different Scanner or object name. Every time I highlight one "readsir" in the program, all the "readsir"s are highlighted. So, if i want to change all the "readsir"s to objIn, is there any way to do that? I am new to eclipse and java programming so I am sorry if it is too obvious.
I went through the program and changed all the words manually, but one of my programs is 700 lines long, so I would like to discover a new way for long programs.
select the variable -> right click->source->refactor. Then type in the new name of the variable and press enter
Or
Select the variable and press, Alt+Shift+R
Assuming you are using eclipse you have a shortcut for that. Highlight the variable name you want to rename and press alt+shift+r, you'll see a box around the name. Rename it and press enter, the name should change everywhere that same variable is used.
If you're using another IDE you'll have a similar solution. Check with a right click or in the menu bar on top and search for a tool named refactor or refactoring.

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.

Eclipse : how we take arguments for main when run

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).

combine multiple windows to a single window

I am developing an application which have the following windows
If the information entered in the windows are correct than only the user will be prompted with the windows in the above sequence. Now the customer has demanded me with this user interface.
Now I have to add all these windows in the last window format, with the specification's as the user will be allowed in the 2nd portion of the last image if the first information entered is correct.And the user when launches this app see the last image and can change the values as any time in the respective portions of the last window.
I have coded it in Swing Java.I am new to Java. I am working in Netbeans 7.1.2 I have three files as
1)Login.java
-containing my LoginDemo class which have main and form object of extended Jframe class
-Login class extending J frame and implementing action listener(this class creates an J frame of next file Enter the information.
2)Algorithm.java
creates new J frame object of next file if information is correct.
3)TravellingSalesmanProblem.java
gives the output as shown in Optimal Travel Route window.
I am accessing the information using REST call to a website.
So can anyone help me in this?
This will depend on how you structured you code. If you simply placed components directly onto the the windows/frames themselves, then you might be in for some work.
Alternatively, if you used panels, which you then placed onto windows, this might save you some time.
Anyway. Assuming you only have windows.
for each window do
myLastWindow.add(window.getContentPane());
This is pretty simple, but you'll also need to know the layout you want. I'd suggest something like GridLayout or VerticalLayout from the SwingX project.

Categories