idea创建第一个java小程序

想5年前,每天不停地敲键盘写java代码,最后决然决定离开编程,5年过去,又回来试试,其中种种难以尽数,且打住,说说现在回来之后的事

5年前最流行的还是eclipse,现在貌似大家都喜欢用idea了,反正自己eclipse也忘得差不多了,就当是重头学,也赶个热门,学学idea。

在https://www.jetbrains.com/help/idea/java-se.html#compile_java_file找到了文档,学习其中“java se>Creating, Running and Packaging Your First Java Application”的内容

哈哈,结果发现没有什么可以写的,直接照着英语文档做下来就好了

Creating a project

Any new development in IntelliJ IDEA starts with creating a project. So let's create one now.

  1. If no project is currently open in IntelliJ IDEA, click Create New Project on the Welcome screen. Otherwise, select File | New | Project.

    As a result, the New Project wizard opens.

  2. In the left-hand pane, select Java. (We want a Java-enabled project to be created, or, to be more exact, a project with a Java module.)
  3. Specify the JDK that you want to use in your project (the Project SDK field). Do one of the following:
    • Select the JDK from the list.
    • If the desired JDK is already available on your computer but is missing from the list, click New and, in the dialog that opens, select the JDK installation directory.
    • Click Download JDK.

    /help/img/idea/2017.2/HWJ004NewProjectJDKSet.png

    Because our application is going to be a "plain old Java application", we don't need any additional technologies to be supported. So, don't select any of the options under Additional Libraries and Frameworks.

    Click Next.

  4. The options on the next page have to do with creating a Java class with a main()method.

    Since we are going to study the very basics of IntelliJ IDEA, and do everything from scratch, we don't need these options at the moment. So, don't select any of the options.

    /help/img/idea/2017.2/HWJ001NewProjectFromTemplate.png

    Click Next.

  5. On the next page, specify the project name (e.g. HelloWorld). If necessary, change the project location suggested by IntelliJ IDEA.

    /help/img/idea/2017.2/HWJ002NewProjectJavaModuleProjectName.png

    Click Finish.

    Wait while IntelliJ IDEA is creating the project. When this process is complete, the structure of your new project is shown in the Project tool window.

Exploring the project structure

Let's take a quick look at the project structure.

/help/img/idea/2017.2/HWJ006ProjectToolWindowInitialState.png

There are two top-level nodes:

  • HelloWorld. This node represents your Java module. The .idea folder and the file HelloWorld.iml are used to store configuration data for your project and module respectively. The folder src is for your source code.
  • External Libraries. This is a category that represents all the "external" resources necessary for your development work. Currently in this category are the .jarfiles that make up your JDK.

(For more information, see Tool Windowsand Project Tool Window.)

Creating a package and a class

Now we are going to create a package and a class. Let the package and the class names be com.example.helloworld andHelloWorld respectively.

Though a package can be created separately, we will create both the package and the class at once.

  1. In the Project tool window, select thesrc folder and press Alt+Insert. (Alternatively, you can select File | New, or New from the context menu for the folder src.)
  2. In the New menu, select Java Class (e.g. by pressing Enter).

    /help/img/idea/2017.2/HWJ010ProjectToolWindowNewClass.png

  3. In the Create New Class dialog that opens, typecom.example.helloworld.HelloWorld in theName field. The Class option selected in the Kind list is OK for creating a class. Press Enter to create the package and the class, and close the dialog.

    /help/img/idea/2017.2/HWJ011CreateNewClassName.png

    The package com.example.helloworld and the class HelloWorld are shown in theProject tool window.

    /help/img/idea/2017.2/HWJ011NewClassInProjectToolWindow.png

    At the same time, the file HelloWorld.java(corresponding to the class) opens in the editor.

    /help/img/idea/2017.2/HWJ012NewClassInEditor.png

Note the package statement at the beginning of the file and also the class declaration. When creating the class, IntelliJ IDEA used a file template for a Java class. (IntelliJ IDEA provides a number of predefined file templates for creating various file types. For more information, see File and Code Templates.)

Also note a yellow light bulb /help/img/idea/2017.2/intentionBulb.png. This bulb indicates that IntelliJ IDEA has suggestions for the current context. Click the light bulb, or press Alt+Enter to see the suggestion list.

/help/img/idea/2017.2/HWJ013IntentionActionsSuggestionList.png

At the moment, we are not going to perform any of the actions suggested by IntelliJ IDEA (such actions are calledintention actions.) Note, however, that this IntelliJ IDEA feature may sometimes be very useful.

Finally, there are code folding markers next to the commented code fragment (/help/img/idea/2017.2/foldingMinus.gif). Click one of them to collapse that fragment. (For more information, seeFolding Code Elements.)

/help/img/idea/2017.2/HWJ014CommentedCodeFolded.png

Writing code for the HelloWorld class

The code in its final state (as you probably know) will look this way:

package com.example.helloworld; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }Copy

The package statement and the class declaration are already there. Now we are going to add the missing couple of lines.

Press Shift+Enter. (In contrast to Enter,Shift+Enter starts a new line without breaking the current one.)

/help/img/idea/2017.2/HWJ015NewLine.png

Using a live template for the main() method

The line

public static void main(String[] args) {}Copy

may well be typed. However, we suggest that you use a different method. Type pand press Ctrl+J.

/help/img/idea/2017.2/HWJ016psvm.png

Select psvm - main() method declaration. (Use the Up and Down arrow keys for moving within the suggestion list, Enterfor selecting a highlighted element.)

Here is the result:

/help/img/idea/2017.2/HWJ017MainMethodInserted.png

IntelliJ IDEA provides code snippets called live templates. psvm is an abbreviation for one of such templates. To insert a live template into your code, you can use Code | Insert Live Template or Ctrl+J. For more information, see Live Templates.

Using code auto-completion

Now, it's time to add the remaining line of code

System.out.println("Hello, World!");Copy

We'll do that using code auto-completion.

Type Sy

The code completion suggestion list is shown.

/help/img/idea/2017.2/HWJ018InsertSystem.png

Select System (java.lang) by pressing Enter.

Type .o and press Ctrl+Period.

/help/img/idea/2017.2/HWJ020InsertOut.png

out is inserted followed by a dot. (You can select an item in the suggestion list by pressing Ctrl+Period. In that case, the selected item is inserted into the editor followed by a dot.)

Type p and then find and selectprintln(String x).

/help/img/idea/2017.2/HWJ022InsertPrintLn.png

IntelliJ IDEA prompts you which parameter types can be used in the current context.

/help/img/idea/2017.2/HWJ023PrintLnInserted.png

Type "

The second quotation mark is inserted automatically and the cursor is placed between the quotation marks. Type Hello, World!

/help/img/idea/2017.2/HWJ024CodeReady.png

The code at this step is ready.

(For more information, see Auto-Completing Code.)

Using a live template for println()

As a side note, we could have inserted the call to println() by using a live template (sout).

If you think that it's enough for live templates, proceed to running the application. Otherwise, try that now as an additional exercise. Delete

System.out.println("Hello, World!");Copy

Type s, press Ctrl+J and select sout - Prints a string to System.out.

The line

System.out.println();Copy

is added and the cursor is placed between( and ).

Type " and then type Hello, World!

Building and running the application

Classes with a main() method can be run right from the editor. To show that, there are the green arrow markers (/help/img/idea/2017.2/run.png) in the left margin.

Click one of the markers and select Run 'HelloWorld.main()'.

/help/img/idea/2017.2/HWJ028RunHelloWorld.png

Wait while IntelliJ IDEA is compiling the class. When the compilation is complete, the Run tool window opens at the bottom of the screen.

/help/img/idea/2017.2/HWJ029RunToolWindow.png

On the first line, there is a fragment of the command that IntelliJ IDEA used to run the class. (Click the fragment to see the whole command line including all options and arguments.) The last line shows that the process has exited normally, and no infinite loops occurred. And, finally, you see the program output Hello, World!between these lines.

(For more information, see Run Tool Window.)

Remarks: building and running applications

Some remarks related to building and running applications in IntelliJ IDEA:

  • Prior to running the class, IntelliJ IDEA has automatically compiled it. When necessary, you can initiate the compilation yourself. The corresponding options can be found in the Build menu.

    /help/img/idea/2017.2/HWJ025BuildMenu.png

    Many of these options are also available as context menu commands in theProject tool window and in the editor.

    Finally, there is an icon in the upper-right part of the workspace that corresponds to the Build Project command (/help/img/idea/2017.2/build.png).

    /help/img/idea/2017.2/HWJ025BuildIcon.png

    For more information, see Build Processand Compilation Types.

  • The options for running and debugging applications can be found in the Runmenu.

    /help/img/idea/2017.2/HWJ030RunMenu.png

    As in the case of the build operations, the run options can also be accessed from the Project tool window and the editor, as well as by means of controls in the upper-right part of the workspace.

    /help/img/idea/2017.2/HWJ031RunToolbarButtons.png

  • Applications in IntelliJ IDEA are run according to what is called run/debug configurations. Such configurations, generally, should be created prior to running an application.

    When you performed the Run 'HelloWorld.main()' command, IntelliJ IDEA, first, created a run configuration and then executed it.

    The name of this run configuration (HelloWorld) is now shown in the run/debug configuration selector to the left of /help/img/idea/2017.2/run.png.

    The HelloWorld configuration now exists as a temporary configuration and, if necessary, you can save it to make it permanent.

    /help/img/idea/2017.2/HWJ032RunConfigSelectorMenu.png

  • Run/debug configurations can do a lot more than just run applications. They can also build applications and perform other useful tasks.

    If you look at the settings for the HelloWorld run configuration (Run | Edit Configurations or Edit Configurationsfrom the run configuration selector), you'll see that the Build option is included by default in the Before launchtask list. That's why IntelliJ IDEA compiled the class when you performed the Run 'HelloWorld.main()' command.

    /help/img/idea/2017.2/HWJ033RunConfigSettings.png

  • If you look at the Project tool window, you'll see that now there is the folderout there. This is the project output folder.

    Inside it is the module output folder (production\HelloWorld), the folder structure for the packagecom.example.helloworld and the compiled class file HelloWorld.class.

    /help/img/idea/2017.2/HWJ027HelloWorldInExplorer.png

    For more information, see Specifying Compilation Settings.

Packaging the application in a JAR

When happy with your application, you may want to package it in a Java archive (JAR) for distribution. To do that, you should create an artifact configuration for your JAR and then build the artifact.

Creating an artifact configuration for the JAR

  1. Select File | Project Structure to open the Project Structure dialog.
  2. Under Project Settings, select Artifacts.
  3. Click /help/img/idea/2017.2/new.png, point to JAR and select From modules with dependencies.

    /help/img/idea/2017.2/HWJ041NewArtifact.png

  4. In the dialog that opens, specify the main application class. (To the right of the Main Class field, click /help/img/idea/2017.2/browseButton.png and selectHelloWorld (com.example.helloworld)in the dialog that opens.)

    /help/img/idea/2017.2/HWJ041NewArtifactCreateJarFromModules.png

    As a result, the artifact configuration is created and its settings are shown in the right-hand part of the Project Structuredialog.

    /help/img/idea/2017.2/HWJ042ArtifactSettings.png

  5. Click OK.

Building the JAR artifact

  1. Select Build | Build Artifacts.

    /help/img/idea/2017.2/HWJ043BuildArtifacts.png

  2. Point to HelloWorld:jar and select Build. (In this particular case, Build is the default action, so you can just press Enter instead.)

    /help/img/idea/2017.2/HWJ044ArtifactBuild.png

    If you now look at the out/artifactsfolder, you'll find your JAR there.

    /help/img/idea/2017.2/HWJ045ArtifactLocation.png

Running the packaged application

To make sure that everything is fine with the JAR, let's run it. To do that, we'll create a JAR Application run configuration and then execute that run configuration.

Creating a JAR Application run configuration

To run Java applications packaged in JARs, IntelliJ IDEA provides the JAR Application run configurations. To create such a configuration:

  1. Select Run | Edit Configurations.
  2. In the Run/Debug Configurations dialog that opens, click /help/img/idea/2017.2/new.png and select JAR Application.

    /help/img/idea/2017.2/HWJ046NewJarAppRunConfig.png

  3. Specify the path to the JAR file. (To the right of the Path to JAR field, click /help/img/idea/2017.2/browseButton.pngand select the JAR file in the dialog that opens.)

    The rest of the settings in this case don't matter, however, there's one more thing that we'll do - just for convenience.

  4. Under Before launch, click /help/img/idea/2017.2/new.png, selectBuild Artifacts and select theHelloWorld:jar artifact in the dialog that opens.

    The Build 'HelloWorld:jar' artifact task is included in the Before launch task list. So each time you execute this run configuration, the artifact will be built automatically.

    /help/img/idea/2017.2/HWJ047JarAppRunConfigSettings.png

Executing the run configuration

  • To the right of the run configuration selector, click /help/img/idea/2017.2/run.png.

    /help/img/idea/2017.2/HWJ048RunJarApp.png

    As before, the Run tool window opens and the application output is shown there.

    /help/img/idea/2017.2/HWJ049JarAppOutput.png

转载于:https://my.oschina.net/xoyo/blog/1476416

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值