Search code examples
javaregexcmdsublimetext3sublime-build

how to build system in sublime text 3 for java which also supports packages?


I have stored all my java projects as follows:

C:\Users\swagat\Documents\JavaProjects\name_of_1st_project\all_source_files.java C:\Users\swagat\Documents\JavaProjects\name_of_2nd_project\all_source_files.java C:\Users\swagat\Documents\JavaProjects\name_of_3rd_project\all_source_files.java C:\Users\swagat\Documents\JavaProjects\name_of_4th_project\all_source_files.java

I hope you understanded the hierarchy.

Now I am trying to make a build system in sublime text 3 which can compile and run any java program in a single click.

My build system contains:

{
    "shell_cmd": "javac $file && java $file_base_name"
}

I also have set my CLASSPATH environment variable to point to C:\Users\swagat\Documents\JavaProjects

I dont want to set different CLASSPATH for each projects

The above setup works if I don't use package in my java source code

But when I use package, for example: package name_of_1st_project; in my java source code, then i get build error that "Could not find or load main class name_of_1st_project. Caused by: java.lang.NoClassDefFoundError: name_of_1st_project/name_of_1st_project (wrong name: name_of_1st_project)"

To solve the issue i modified the build file as:

{
    "shell_cmd": "javac $file && java $file_base_name.name_of_1st_project"
}

And it solves the issue but just for the 1st project.

I want to make it dynamic using regex or whatever which can append the ".name_of_nth_project" at the end of the sublime build system file. I read the sublime docs and found no variable which has the current folder name(just the name, excluding the path) which I can use.

I searched the whole internet but found no exact solution. Please help.(maybe my comments can help)


Solution

  • Change the sublime build file as:

    {
        "shell_cmd": "javac $file && java $file_base_name.$file_base_name"
    }
    

    But the folder name and the package name should be the same as the file name containing the main method.