0

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)

  • If packages are used, the the program can be executed by: java pkg1.pkg2.pkg3.file_name and I just want to do the same in sublime build to execute the program – Swagat dash May 07 '20 at 10:36
  • 1
    If you're at the point where you have multiple Java projects and want to have unified builds for them, then it's probably time to investigate and use a proper build tool (and then sublime just needs to execute a task of the build tool and not know about the details of how to compile stuff). You're effectively trying to build a poor mans build system. Gradle is a popular one in the Java world right now, as is the slightly older Maven. – Joachim Sauer May 07 '20 at 10:36
  • I dont know how to implement this working regex in sublime build which can filter me just the folder name from the path to the folder. The regex can be found in https://regex101.com/r/qA9eZ1/89 . Please help me implement this regex in sublime build to filter the folder name from the path ( path to the folder is a sublime's system variable named "$file") – Swagat dash May 07 '20 at 11:12

1 Answers1

0

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.