简体   繁体   中英

How do I run/debug a single .java file in IntelliJ?

I have a simple java file as follows, which prints to the console:

import java.io.*;
import java.lang.reflect.*;
import java.lang.Math.*;

public class CodingPrep {

    public static void main(String[] args) {

        System.out.println("Hello World");

    }
}

I'm trying to run this file via IntelliJ, WITHOUT creating any project, etc. (There is a reason for this however it's beyond the discussion here). How can I do that ?

So far I've just installed IntelliJ and have linked a Java JDK with it. When I try to run or debug this file, I get re-routed to the Edit Configurations Box, where there's an error showing: "No module specified" .

在此处输入图片说明 Note that my code is actually more than just a "Hello World", however it's all command prompt based, where I have to pass arguments to my calls to the executable, and also have to provide input (via ReadLine ) while the program is running.

I need an IDE so that I can debug my java files using breakpoints, etc.

What do I do ?

This is not possible with IDEA, because in order to run a class it needs some basic configuration, like JDK, classpath, output path etc.

So you must create and configure a module and put the class file in it. Then, when running it, a command prompt terminal will be opened in the IDE itself allowing you to enter the input.

To solve the error: "No module specified" . Write the name of your class in text box next to Main class in opened Edit Configuration window.

Here, you put CodingPrep in text box next to Main class: in opened Edit Configuration window.

It will work like charm.

If you already have some other project opened then by default new single java file will be treated as part of existing project.

To run single java file (CodingPrep.java in your case), you have to open the Edit Configuration and there, add a new configuration for Java Scratch, where you provide the below items:

  • path to scratch file ( /home/user/path/to/CodingPrep.java )
  • Main class ( CodingPrep.java )
  • Working Directory
  • Additionally you can save this config with name CodingPrep or any other name.

Apply and Save and you are good to go. Check the sample Edit Configuration here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM