Sublime Forum

OS X: How to run java code in Sublime Text 3

#1

Hey guys, I hope you can help me out on that issue.
In order to run java code in Sublime Text 3 I followed the instructions to edit JavaC.sublime-build. If I run my code in ST3 now I get the error message that my main class couldn’t be found or loaded. So I tried to combine the instruction above with this, adding the jdk path to JavaC.sublime-build. But I still can’t run java code with ST3. I’ve read something about classpath but I thought it would be solved with that extra line from the second source, referring to the jdk bin directory.

What I’m doing wrong?

Thanks for your help.

I’m running Sublime Text 3 on Mac OS X El Capitan.

Edit: I have edited the JavaC.sublime-buid to:
`{
“shell_cmd”: “javac “$file””,
“path”: “/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin:$PATH”,
“file_regex”: “^(…?):([0-9]):?([0-9]*)”,
“selector”: “source.java”,

"variants":
[
    {
        "name": "Run",
        "shell_cmd": "java $file_base_name"
    }
]

}`

And I get: > Error: couldn’t find or load main class startJava

[Finished in 0.7s with exit code 1]
[shell_cmd: java startJava]
[dir: /Users/dkin/Documents/Coding/java]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

0 Likes

#2

Broadly speaking, in Java a class is represented by a file and a package is represented by a folder, which means that in order to find your classes, the java run time needs to know what folders to look in. Java has the notion of a class path that gives it a list of all of the locations where java packages (and thus classes) can be found.

This applies even if you don’t specifically define a package for your class. For example:

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

It’s hard to say which of these might be kicking you in the pants because you didn’t post the actual java code, but as a first step you might want to add a working_dir setting in your build file to see if that helps. That would at least make sure that java is running in the same folder as the compiled class file.

There is information about that in the following forum post:

1 Like

#4

Thanks for your response but your sample code isn’t working either.
I get the same error message as in my thread starting post.

0 Likes

#5

Maybe a stupid question but it isn’t obvious from your post. You did compile a source before running a program? Run variant expects a compiled class, not its source file.

0 Likes