monochromatic

monochromatic blog: http://blog.z3bra.org
git clone git://z3bra.org/monochromatic
Log | Files | Refs

java-without-eclipse.txt (6725B)


      1 
      2 # [Java without Eclipse](#)
      3 ## — 08 September, 2013
      4 
      5 This is a fact, when someone starts developping with java, he (or she) is given
      6 an IDE: Eclipse.  
      7 
      8 When one codes in C, using plain gcc + Makefile and a good text editor is
      9 adviced.  This is the same for C++, bash, ruby, haskell, perl, python, bla bla
     10 bla...  
     11 Why would java escape the rules ?!  
     12 
     13 * It is hard to play with classpath.
     14 * It is boring to compile java by hand
     15 * Eclipse is SOOOOOOO good at it !
     16 * Eclipse has plenty of plugin to manage a project
     17 * bla bla bla...
     18 
     19 Of course, there are advantages ! And if these are good enough for you. Go with
     20 eclipse And have some fun !  
     21 
     22 ### Throwing Eclipse away
     23 
     24 For that to be clear, **I am not saying that IDEs are evil ! I am just showing
     25 how you can avoid them, in favor of your daily tools**.  
     26 I just finished a java project for my CS studies, on a bare notebook (without
     27 X.org). And I had NO PROBLEM with not using Eclipse at all !  
     28 Now let me introduce my setup. Of course, it's not as easy as double-clicking
     29 on the eclipse-setup.exe. but it follows my ideas, and I'm happy with it !  
     30 
     31 It needs the following:
     32 
     33 * Your editor of choice
     34 * JRE (O RLY?)
     35 * JDK (U DON'T SAY)
     36 * Know your shit !
     37 
     38 You just have to know how to compile/run a java project.  Here is what I
     39 learnt:  
     40 **javac** is used for compiling, **java** to run an application.  
     41 javac takes a .java file, and compiles it into a .class file, that you will run
     42 with java. Who needs Eclipse to do that ?!  
     43 
     44 <q>But real-life project are -a lot- more complicated than that !  You have to
     45 put files in packages, classes in another directory, etc...</q>  
     46 
     47 And you just activated my trap card...  
     48 For sure, real project are a lot more complicated ! For example:  
     49 
     50     ~/src/java/eg
     51     ├── bin
     52     ├── build
     53     │   └── build.xml
     54     ├── conf
     55     │   └── server-log4j.properties
     56     ├── lib
     57     │   ├── junit-4.11.jar
     58     │   └── log4j-1.2.12.jar
     59     ├── src
     60     │   └── com
     61     │       └── irc
     62     │           ├── client
     63     │           │   ├── ClientConnexion.java
     64     │           │   ├── ClientFenetre.java
     65     │           │   ├── Client.java
     66     │           │   └── Client_TEST.java
     67     │           ├── server
     68     │           │   ├── ClientRun.java
     69     │           │   ├── Server.java
     70     │           │   └── Server_TEST.java
     71     │           └── utils
     72     │               ├── Command.java
     73     │               ├── Command_TEST.java
     74     │               ├── Communication.java
     75     │               ├── Communication_TEST.java
     76     │               └── Log.java
     77     └── tags
     78 
     79 This is what my project looks like. sources are in src/, classes compiled in
     80 bin/. Each source has it's own package.  
     81 
     82 Let's see how to handle this project, using UNIX as your IDE !
     83 
     84 ### Compilation
     85 
     86 It probably is the hardest part. Java is hard to compile properly, so tools
     87 like Ant are used in IDEs.  
     88 
     89 Basicaly, compilation goes like this:
     90 
     91     javac -d bin/ -sourcepath src/ -classpath bin/ src/com/irc/utils/Command.java
     92 
     93     -d specifies the directory where your classes will go.  
     94     -sourcepath tells javac where to search for source files.  
     95     -classpath is the same as the abode, but for class files.  
     96 
     97 Do you see it coming ? **YES**, we will use a Makefile.  
     98 Here is mine:
     99 
    100     # Locate directories
    101     class_d=bin
    102     lib_d=lib
    103     source_d=src
    104     package_d=com/irc
    105 
    106     # Compilation stuff
    107     JAVAC=javac
    108     JFLAGS=-g -d $(class_d) -sourcepath $(source_d) -Xlint:all
    109 
    110     classpath:=$(class_d):$(lib_d)/junit-4.11.jar:$(lib_d)/log4j-1.2.12.jar
    111 
    112     # If there's already a CLASSPATH, put it on the front
    113     ifneq ($(CLASSPATH),)
    114             classpath:= $(CLASSPATH):$(classpath)
    115     endif
    116 
    117     # Re-export the CLASSPATH.
    118     export CLASSPATH:=$(classpath)
    119 
    120     MATCH='.*[^(_TEST)]\.java'
    121 
    122     ifdef TEST
    123         ifeq ($(TEST), all)
    124             MATCH='.*_TEST\.java'
    125         else
    126             MATCH='.*\/$(TEST)\/.*_TEST\.java'
    127         endif
    128     endif
    129 
    130     # Find all the source and convert them to class files
    131     S_SERVER= $(shell find $(source_d)/com/irc/server -regex $(MATCH))
    132     C_SERVER= $(patsubst src/%.java, bin/%.class, $(S_SERVER))
    133     S_CLIENT= $(shell find $(source_d)/com/irc/client -regex $(MATCH))
    134     C_CLIENT= $(patsubst src/%.java, bin/%.class, $(S_CLIENT))
    135     S_UTILS= $(shell find $(source_d)/com/irc/utils -regex $(MATCH))
    136     C_UTILS= $(patsubst src/%.java, bin/%.class, $(S_UTILS))
    137 
    138     .SUFFIXES: .java .class
    139     .PHONY: default server client utils clean init all
    140 
    141     default: classes
    142 
    143     $(class_d)/%.class: $(source_d)/%.java
    144     	@echo "JAVAC $<"
    145     	@$(JAVAC) $(JFLAGS) $<
    146 
    147     classes: utils server client $(class_d)
    148 
    149     server: $(C_SERVER) utils
    150     client: $(C_CLIENT) utils
    151     utils: $(C_UTILS)
    152 
    153     all: init classes
    154 
    155     $(class_d):
    156     	mkdir $(class_d)
    157 
    158     clean:
    159     	rm -rf $(class_d)/*
    160 
    161 It will search the appropriate sources in your sourcepath, and compile them in the
    162 associated classpath.  
    163 Building the tests appart is done with `make TEST=[server|client|utils|all]`
    164 And it will compile your code only if it has changed (thanks GNU/make !).  
    165 
    166 It there you need more explaination on this, mail me your questions !
    167 
    168 ### Running
    169 
    170 Running your program will be a lot more easier. And you probably know What I'm
    171 going to tell you: _shell script_.  
    172 
    173 Simple huh ? You just need to know how to run a java program:  
    174 
    175     java -classpath bin/ com.irc.server.Server
    176 
    177 Do I really need to explain what -classpath is ..?
    178 On the other hand, "com.irc.server" is the package containing my Server class,
    179 and "Server" is the class containing the method main().  
    180 
    181 So you will have no problems building the correct script that will run your
    182 application. It will probably (in its simplest form) look like this:  
    183 
    184     #!/bin/sh
    185 
    186     classpath=bin:lib/log4j-1.2.12.jar
    187     package=com.irc.server
    188 
    189     java -cp $classpath $package.Server
    190 
    191 Isn't that easy ?
    192 
    193 ### Conclusion
    194 
    195 **YOU. DON'T. NEED. ECLIPSE. AT ALL.**
    196 
    197 I know that a lot of people will discard me, telling that This is a pain in the
    198 neck to go like this, that you need to know Makefile, and take the time to
    199 write them, etc... But these are reusable, and it helps you to know how to
    200 manage the whole process ! Furthermore, it can be interesting to (at least) try
    201 this method, just to see how it goes.  You will be able to use every tool you
    202 want, git, vim, emacs, svn, ctags, ...  
    203 
    204 No needs to get used to the Eclipse interface, and to forget about your
    205 favorite editor ! Isn't that promising ?
    206 
    207 <!-- vim: set ft=markdown ts=4 et: -->