简体   繁体   中英

Error running Java on Sublime Text [OSX]

So I've tried to start compiling and running java on sublime, and it works fine if the package is not defined.

this compile and run:

public class Tester
{
    public static void main(String[] args)
    {
        System.out.println("this is a test.");  
    }
}

But if I add a package:

package test;

public class Tester
{
    public static void main(String[] args)
    {
        System.out.println("this is a test.");  
    }   
}

I got this error

Error: Could not find or load main class Tester
[Finished in 6.8s with exit code 1]
[cmd: ['javac "Tester.java" && java "Tester"']]
[dir: /Users/ph/Documents/JAVA/test]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Any idea why is this happening or how to fix it?

  1. [cmd: ['javac "Tester.java" && java "Tester"']]
  2. [dir: /Users/ph/Documents/JAVA/test]

Sublime Text is trying to compile your program in a directory called "test" (see #2), which is the package name. It is looking for a file named "Tester.java" within that directory (see #1), but it doesn't exist because "Tester.java" is inside the current directory ("JAVA").

When compiling Java files in packages, the files need to be in a directory structure that reflects the package hierarchy. So you need to move your file to the directory that corresponds to the package it is contained in. For example, class "A" in package example.utils.letters would have to exist at the path ../example/utils/letters/A.java

Create folder "JAVA/test" and move Tester.java into there, then run it.

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