Question

How do you run code in a .txt file?

I know you're supposed to use a terminal, but it keeps giving this message:

javac: file not found: hii.java
Usage: javac <options> <source files>
use -help for a list of possible options

I saved my .txt file on the desktop.

Was it helpful?

Solution

  1. Create a file with extension .java, for example Hi.java.

  2. Inside your Hi.java file, create something like:

    public class Hi {
        public static void main(String[] args) {
            System.out.println("Hi");
        }
    }
    
  3. Open a terminal in the directory where your Hi.java file is located (in your example in the desktop).

  4. In the terminal, run the command:

    javac Hi.java
    

The file Hi.class should be created in your current directory (Desktop).

  1. In the terminal, run the command:

    java Hi
    

Note that:

In Java files should terminate with .java, and it is best practice that a file name should start with a capital letter. You cannot use a file with extension .txt or any other extension. If you want to run a Java class then a .java filename extension is required and is not optional.

OTHER TIPS

After reading this, I cannot even think of where to start.

Just try something like JCreator Lite Edition (LE), and try to work in it for a bit.

While it is loading, I suggest reading something like this. You have to be in the same working directory as your file when you run that command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top