Search code examples
javajarcompilationexecutable-jarworkspace

How to set up a Java workspace without an IDE


I usually code with IDEs like Intellij Idea or Eclipse, but due to several reasons, I no longer have access to an IDE, and I'd like to be able to code in Java on a remote Linux machine through a ssh terminal. Making simple progams with only a few classes is easy, but some of my projects have several libraries and several separate .java files. I also need to export to a .jar file.

For example, I have the following file organisation:

project/
   src/
      a.java
      b.java
      c.java
   libs/
      lib1.jar
      lib2.jar
   out/
      export_here.jar
   someconfig.conf

The java app consists of the a, b, and c .java files, uses libraries lib1 and lib2, and the file someconfig.conf needs to be inside the export jar.

I want to know how to easily compile and build a project such as this.

In other words, I just want to know how to export my project into a runnable jar the right way.

I expect this can be done with a few commands. If so, I plan to make a shell script to automate everything.

Thanks in advance!


Solution

  • As suggested by other users you need to use a build management tool to do this like Ant, Maven etc. I have used Ant quiet frequently to do these kind of automated tasks. In order to install and Use Ant you can refer How To Install Apache Ant

    After that the main task is to write your automation script and that is called a build xml in ant world. Here is a sample build.xml file you can refer to start with:

    <project>
    
        <target name="clean">
            <delete dir="build"/>
        </target>
    
        <target name="compile">
            <mkdir dir="build/classes"/>
            <javac srcdir="src" destdir="build/classes"/>
        </target>
    
        <target name="jar">
            <mkdir dir="build/jar"/>
            <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
                <manifest>
                    <attribute name="Main-Class" value="oata.HelloWorld"/>
                </manifest>
            </jar>
        </target>
    
        <target name="run">
            <java jar="build/jar/HelloWorld.jar" fork="true"/>
        </target>
    
    </project>
    

    And for more information on the above sample You can visit this

    In General you can read more at How to create build.xml

    After creating your build.xml you can run ant by ant <path to build.xml> or ant in case your build.xml lies in current directory

    Hope this helps you in right direction