[JAVA] Super basic usage of Eclipse

PE-BANK Kansai Branch 2017-04-15 This material is used in the Java study session. The content is for super beginners.

Install Pleiades All in One Eclipse

Please download * Java Full Edition * according to your environment from the following. http://mergedoc.osdn.jp/pleiades_distros4.6.html

It will be one of the red frames in the image below. 2017-04-15-pleiades.png

For Windows

If you unzip the zip file as it is in the download folder etc., the path length will exceed the allowable range of Windows, and an error may occur.

The following steps are reliable.

It may be useful to create a shortcut on your desktop to C: \ App \ pleiades \ eclipse \ eclipse.exe.

For Mac

Please install by referring to the following page.

Eclipse Pleiades All in One for Mac released

Right-click on the first boot-> select Open. A security warning dialog will be displayed. Click Open.

About Eclipse terminology

Please refer to the following pages for the terms that you want to keep at a minimum.

[About Eclipse terms "workbench" "perspective" "editor" "view" "workspace"](http://www.hitachi.co.jp/Prod/comp/soft1/cosminexus/useful/tips/081202_eclipse-workbench -workspace.html)

Start Eclipse

Specifying a workspace

When you start Eclipse, you will be asked where to save your "workspace".

There is a check box that says "Do not show again next time", but I think it is better to show it until you get used to Eclipse.

Initial setting

When Eclipse starts, compare the actual screen with the "Eclipse terminology" mentioned earlier.

People with narrow screens don't use "Outline View" and "Problem View" for the time being, so let's minimize them.

In this study session, some people are using Windows and some are using Mac, so the character code is ʻUTF-8` so that problems will not occur when the sample code is imported in the future. I would like to unify to.


Creating a simple program using Eclipse

Specifications of the program to be created

This time, create a program that receives a number from the argument and outputs the result of squared the value.

Creating a project

First, let's create a project. Right-click on Package Explorer and select New-> Java Project.

The project name can be anything. Let's call it JavaStudy001 for the time being.

Leave the other items at their defaults and click Next.

The Java build setting screen will be displayed, but this time, leave the default and click "Finish".

A JavaStudy001 folder is created in the workspace specified at startup. With various configuration files used by eclipse The src folder, which stores the source code, A bin folder is created to store the generated class files.

Creating a class

Create a class so that it has the following package and class structure.

Creating a Calculation class

Calculation.java


package myPackage.calc;

public class Calculation {

}

Creating a Square class

Square.java


package myPackage.calc;

public class Square extends Calculation {

}

Creating a Program class

Program.java


package myPackage.main;

public class Program {

	public static void main(String[] args) {
		//TODO auto-generated method stub

	}

}

I think that the TODO comment is included and the part of TODO is emphasized. If you include a TODO comment in your code, it will be listed in the Task View. There is a function to jump from the "task view" to the relevant comment, so if you need to implement it later or ask another developer to implement it, leave the TODO comment.

The Task View can be viewed by clicking Window-> Show View-> Task.

Implementation of Calculation class

Let's define a value field that holds the value.

package myPackage.calc;

public class Calculation {

	protected int value = 0;

}

Then right-click on value and click Source-> Generate getters and setter.

In the Generate Getters and Setters dialog, leave the defaults and click ʻOK`.

Make sure that the getValue () and setValue (int value) methods are generated.

Next, create a result field that holds the calculation result. Let's add the definition protected int result = 0; directly under the value field.

In addition, create a ʻoutputmethod that outputs the calculation result. Let's make this aspublic` so that it can be called from external classes.

Eclipse has the ability to complement your code.

public void output() {
  System.
}

↑ If you wait for a while in this state, candidate items will be displayed. Select ʻoutand enter.. The members of the ʻout class are displayed, so select println.

Finally, pass this.result to the println method.

Finally, make sure that it looks like this:

Calculation.java


package myPackage.calc;

public class Calculation {

	protected int value = 0;

	protected int result = 0;

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public void output() {
		System.out.println(this.result);
	}

}

Java has a mechanism to generate an API specification in HTML format based on comments in the source code called Javadoc.

Let's add a Javadoc comment to the ʻoutput` method.

Javadoc is a comment that starts with / ** and ends with * /. By convention, if a comment spans multiple lines, put a * at the beginning of the second and subsequent lines.

In Eclipse, typing / ** and pressing Enter will automatically insert a Javadoc comment.

/**
 *The calculation result is output to the console.
 */
public void output() {

}

Let's add a Javadoc comment to getValue as well.

/**
 * 
 * @return
 */
public int getValue() {
  return value;
}

Then, @return will be automatically generated as shown in ↑. This is called the Javadoc tag, where @ return describes the return value of the method.

If you use Javadoc tags to describe the method definition, Javadoc will format it neatly when you generate HTML.

/**
 *Gets the value used in the calculation.
 * @The value used for the return calculation
 */
public int getValue() {
  return value;
}

Let's add Javadoc to setValue as well.

/**
 *Set the value used for the calculation.
 * @param value The value used for the calculation
 */
public void setValue(int value) {
  this.value = value;
}

@ param is an argument tag.

You can also add Javadoc comments to classes and fields. It will also be displayed when you complete the code in Eclipse, so be sure to write it aggressively.

Square class implementation

Define a calculate method that calculates the square.

Square.java


package myPackage.calc;

public class Square extends Calculation {

	/**
	 *Square the value.
	 */
	public void calculate() {
		//Square value
		this.result = this.value * this.value;

		//Output the result
		this.output();
	}

}

Implementation of Program class

Finally, implement the main method. Open the "Task View" and double-click on the relevant TODO comment.

Implement as follows.

Program.java


package myPackage.main;

public class Program {

	public static void main(String[] args) {
		//TODO auto-generated method stub

		Square square = new Square();
	}

}

You should see a red squiggly line in the Square class. This is a notation that there is an error in that part, and it is because the myPackage.calc.Square class cannot be seen.

Hover your mouse over the wavy line and wait for a while to see "Quick Fix". Click'Import'Square' (myPackage.calc) `.

ʻImport myPackage.calc.Square;` is added and the red squiggles disappear.

You will then see a yellow wavy line in square. This is a notation that there is a warning in the relevant part. The content is that the square variable defined in the program is not used. It will disappear as you proceed with the implementation, so proceed as it is.

public static void main(String[] args) {
  //TODO auto-generated method stub

  Square square = new Square();

  square.setValue(Integer.parseInt(args[0]));
}

The parameters given at program startup are stored in the main method ʻargs. This time we convert the first passed parameter to ʻint and set it to value in Square.

Hover your mouse over the setValue method to see the contents of the Javadoc comment.

Furthermore, if you want to check the contents of setValue, right-click and select" Open Declaration "to jump to the place where the corresponding method is defined.

Finally, the calculation is performed and the result is output.

public static void main(String[] args) {
  //TODO auto-generated method stub

  Square square = new Square();

  square.setValue(Integer.parseInt(args[0]));

  square.calculate();
}

Now that the implementation is complete, let's delete the TODO comment.

Run

Click the Run button (a round green with a white triangle). The "Running Configuration" dialog is displayed. Select "Java Application" and click the "New Launch Configuration" button.

Make sure that is, and click Run.

The console will appear and you will see something like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at myPackage.main.Program.main(Program.java:11)

ʻArrayIndexOutOfBoundsException` is a * exception * that occurs when the number of elements in the array specified when accessing the array exceeds the number of elements.

This time I got ʻargs [0] and the first element of the array, but since I ran it without specifying any parameters, the array of ʻargs is empty.

Let's change the "execution configuration" so that arguments are given at runtime.

Confirm that the console is displayed and the output is 9.

debug

You may want to check the processing process, such as when the calculation result is different from the intended content. You can set a * breakpoint * in the source code to pause the process at that point.

Let's set a breakpoint on the 10th line of Square.java, where value is squared. Double-click on the line number. A blue circle is set to the left of the line number.

Then click the bug icon button. "Confirm perspective switching" is displayed. Click "Yes".

The Debug Perspective is displayed and the line where you just set the breakpoint is highlighted. You can see the contents of the variable by hovering over this.result or this.value.

Click the step over button to process that line and move to the next line. If you mouse over this.result again, you can see that the calculation result is stored in the variable.

With the line this.output (); highlighted, click the Step In Button. The ʻoutput method of Calculation.javais displayed and theSystem.out.println (this.result);` line is highlighted.

Click the Resume button to process the continuation at once until the next breakpoint.

When you're done debugging, click the button labeled Java in the upper right to return to the Java Perspective.


I explained the basic operation of Eclipse. You will find it very convenient compared to creating a program with a text editor.

Recommended Posts

Super basic usage of Eclipse
swift CollectionView Super basic usage
Basic usage of java Optional Part 1
Basic usage of enums and code examples
Introduction and basic usage of Simple Calendar gem
Java review ③ (Basic usage of arrays / reference type)
Minimal usage of Mockito
Basic format of Dockefile
Japanese localization of Eclipse
Basic methods of Ruby hashes
[Java] Mirage-Basic usage of SQL
Basic knowledge of SQL statements
Basic methods of Ruby arrays
Basic usage notes for Jackson
[Docker] Introduction of basic Docker Instruction
[Ruby] List of basic commands
Summary of basic functions of ImageJ
Update timing of Eclipse org.eclipse.wst.common.component
Web system construction (super basic) ④: Web system construction
Review of Ruby basic grammar
Understand the basic mechanism of log4j2.xml
Basic knowledge of Ruby on Rails
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
[Rails] Differences and usage of each_with_index and each.with_index
From introduction to usage of byebug
Basic processing flow of java Stream
Spring Security usage memo Basic / mechanism
[Basic knowledge of Java] Scope of variables
[Specific usage of before_action] Rails refactoring
Basic structure of Java source code