user5425586
user5425586

Reputation:

How to make a package-supporting java compiler?

Is there any way to create a java compiler/program runner in Batch that can automatically compile Java source files that are part of packages? I have been all over stack overflow for this, and have found absolutely nothing. I am currently using Notepad++. Please note that I am doing this just to gain a better understanding of the java build process.

Right now, I have this basic compiler/runner set up :

javac -d ..\..\bin $(FILE_NAME)
java -classpath ..\..\bin $(NAME_PART)

Do note that I am using Notepad++-specific variables, like $(FILE_NAME) and $(NAME_PART). You get the basic idea though. I do know how to run packaged classes from the command line (java .), but I have no clue as to how to automate it. I would like it to output this result:

javac javac -d ..\..\bin $(FILE_NAME)
java -classpath ..\..\bin [packagenamevariable].$(NAME_PART)

Thanks for considering this question!

Upvotes: 1

Views: 112

Answers (2)

zapl
zapl

Reputation: 63955

Because I find it much simpler then ant

A very minimal gradle setup looks like:

.
├── build.gradle
└── src
    └── main
        └── java
            └── com
                └── foo
                    └── bar
                        └── Main.java

where build.gradle contains

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.foo.bar.Main"

and Main.java

package com.foo.bar;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

With gradle installed you can simply call gradle run and the following happens:

$ gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Hello world

BUILD SUCCESSFUL

Total time: 0.836 secs

there are other tasks besides run. See https://docs.gradle.org/current/userguide/application_plugin.html

One useful task is installDist that creates a .bat file to start just the code in ./build/install/{projectname}/bin/{projectname}.bat. That way you can start the code without all the gradle noise.

It's also possible to use gradle without installing it on your system by using gradle wrapper: https://docs.gradle.org/current/userguide/gradle_wrapper.html

  • replace gradle in above commands with gradlew.bat
  • the easiest solution to get the required files (gradlew.bat and the files in /gradle/wrapper) is to steal them from some project that has them. Picked randomly from github: https://github.com/quinnliu/sampleGradleProject

Upvotes: 0

Stephen C
Stephen C

Reputation: 719709

I have been all over stack overflow for this, and have found absolutely nothing.

That's because this is really hard to write a general purpose Java compile/run framework using just BAT scripts.

And unnecessary.

I am currently using Notepad++, and do NOT wish to change to IDE currently.

That's fine. You don't have to use an IDE.

Instead, you can install a Java-aware build tool such as Ant, Maven or Gradle.

(For a small project with minimal library dependencies, Ant will work nicely. For larger projects, Maven or Gradle would be better.)

Upvotes: 3

Related Questions