Skip to content

Instantly share code, notes, and snippets.

@sakjur
Last active August 29, 2015 14:00
Show Gist options
  • Save sakjur/11264986 to your computer and use it in GitHub Desktop.
Save sakjur/11264986 to your computer and use it in GitHub Desktop.
Java for non-Java programmers

Java for non-Java Programmers

I was recently faced with the issue of programming Java, and as a Python and C user, my impression of Java was, too say the least, not quite good. The main issue for me is not really the language in itself, but rather the inconvenience of having to use a Java IDE rather than my preferred editor.

Trying to achieve the quite spectacular achievement in not using NetBeans, Eclipse or IntelliJ for programming Java was.. interesting. The first thing I knew I needed was a build system, as I wouldn't be able to stay sane trying to manually keep track of compiling all the files at changes.

Build System: Rake

I wasn't sure

<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>

felt like the most convenient way of writing a build script. Maybe I'm just being silly - but I really don't like handwriting XML :P

"I know make! Let's use make" I thought. Googling for make java turned up with a post on Why no one uses make with Java. So that didn't seem like the best idea.

Eventually (after looking at Maven and Gradle too) I decided for rake with rakejava (I looked at raven too, but we weren't agreeing)

I am really quite happy with the performance and read-/write-ability rake were providing me with, in a project with ~550 lines of code, spread over 13 files (or so), initial compiling took me about 1.4 seconds, while compilation took only 0.08 seconds when no files had changed so the performance is really good for smaller projects at least. Building a jar-file added only a few hundreth of a second to the time, so for my code, rake wasn't wasting any time.

It was quite easy for me to add functionality for generating HTMLs and PDFs I needed for the presentation of the project.

Eventually, my rake file contained the following data:

require 'rakejava'
require 'rake/clean'

CLEAN.include 'classes'
CLEAN.include 'cachesim.jar'
CLEAN.include 'docs'

task :default => :compile

task :jar => "cachesim.jar"

jar "cachesim.jar" => :compile do |t|
    t.files << JarFiles["classes", "**/*.class"]
    t.main_class = "is.mjuk.cache.CacheSimulator"
    t.manifest = {:version => '0.0.1'}
end

javac :compile => "classes" do |t|
    t.src << Sources["source/", "**/*.java"]
    t.dest = 'classes'
end

javac :compiletest => :compile do |t|
    t.src << Sources["source/", "**/*.java"]
    t.src << Sources["test/", "**/*.java"]
    t.classpath << Dir["/usr/share/java/junit4.jar"]
    t.dest = 'classes'
end

task :test => :compiletest do |t|
    sh "java -cp classes:/usr/share/java/junit4.jar \
        org.junit.runner.JUnitCore is.mjuk.cache.TestMain"
end

task :run => :compile do |t|
    sh "java -cp classes is.mjuk.cache.CacheSimulator"
end

task :doc => "docs" do |t|
    sh "javadoc -sourcepath source/ -d docs/ is.mjuk.cache"
end

task :testdoc => "docs" do |t|
    sh "javadoc -sourcepath test/:source/ -d docs/ is.mjuk.cache"
end

task :pdf => [:testpostscript, :postscript] do
    sh "ps2pdf docs/source.ps docs/source.pdf"
    sh "ps2pdf docs/test.ps docs/test.pdf"
    sh "pdfunite docs/source.pdf docs/test.pdf docs/cachesim.pdf"
end

task :postscript => "docs" do
    sh "enscript -M A4 -q -b '$n|%N %W|$V$%/$=($p)'\
     -Ejava -C --toc -p - source/is/mjuk/cache/*.java > docs/source.ps"
end

task :testpostscript => "docs" do
    sh "enscript -M A4 -q -b '$n|%N %W|$V$%/$=($p)'\
     -Ejava -C --toc -p - test/is/mjuk/cache/*.java > docs/test.ps"
end

directory "classes"
directory "docs"

JUnit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment