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