1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
<!--
__COPYRIGHT__
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<para>
So far, we've been using examples of
building C and C++ programs
to demonstrate the features of &SCons;.
&SCons; also supports building Java programs,
but Java builds are handled slightly differently,
which reflects the ways in which
the Java compiler and tools
build programs differently than
other languages' tool chains.
</para>
<section>
<title>Building Java Class Files: the &b-Java; Builder</title>
<para>
The basic activity when programming in Java,
of course, is to take one or more <filename>.java</filename> files
containing Java source code
and to call the Java compiler
to turn them into one or more
<filename>.class</filename> files.
In &SCons;, you do this
by giving the &b-link-Java; Builder
a target directory in which
to put the <filename>.class</filename> files,
and a source directory that contains
the <filename>.java</filename> files:
</para>
<scons_example name="java">
<file name="SConstruct" printme="1">
Java('classes', 'src')
</file>
<file name="src/Example1.java">
public class Example1
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="src/Example2.java">
public class Example2
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="src/Example3.java">
public class Example3
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
</scons_example>
<para>
If the <filename>src</filename> directory contains
three <filename>.java</filename> source files,
then running &SCons; might look like this:
</para>
<scons_output example="java">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
<para>
&SCons; will actually search the <filename>src</filename>
directory tree for all of the <filename>.java</filename> files.
The Java compiler will then create the
necessary class files in the <filename>classes</filename> subdirectory,
based on the class names found in the <filename>.java</filename> files.
</para>
</section>
<section>
<title>How &SCons; Handles Java Dependencies</title>
<para>
In addition to searching the source directory for
<filename>.java</filename> files,
&SCons; actually runs the <filename>.java</filename> files
through a stripped-down Java parser that figures out
what classes are defined.
In other words, &SCons; knows,
without you having to tell it,
what <filename>.class</filename> files
will be produced by the &javac; call.
So our one-liner example from the preceding section:
</para>
<scons_example name="java-classes">
<file name="SConstruct" printme="1">
Java('classes', 'src')
</file>
<file name="src/Example1.java">
public class Example1
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
public class AdditionalClass1
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="src/Example2.java">
public class Example2
{
class Inner2 {
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
}
</file>
<file name="src/Example3.java">
public class Example3
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
public class AdditionalClass3
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
</scons_example>
<para>
Will not only tell you reliably
that the <filename>.class</filename> files
in the <filename>classes</filename> subdirectory
are up-to-date:
</para>
<scons_output example="java-classes">
<scons_output_command>scons -Q</scons_output_command>
<scons_output_command>scons -Q classes</scons_output_command>
</scons_output>
<para>
But it will also remove all of the generated
<filename>.class</filename> files,
even for inner classes,
without you having to specify them manually.
For example, if our
<filename>Example1.java</filename>
and
<filename>Example3.java</filename>
files both define additional classes,
and the class defined in <filename>Example2.java</filename>
has an inner class,
running <userinput>scons -c</userinput>
will clean up all of those <filename>.class</filename> files
as well:
</para>
<scons_output example="java-classes">
<scons_output_command>scons -Q</scons_output_command>
<scons_output_command>scons -Q -c classes</scons_output_command>
</scons_output>
<para>
To ensure correct handling of <filename>.class</filename>
dependencies in all cases, you need to tell &SCons; which Java
version is being used. This is needed because Java 1.5 changed
the <filename>.class</filename> file names for nested anonymous
inner classes. Use the <varname>JAVAVERSION</varname> construction
variable to specify the version in use. With Java 1.6, the
one-liner example can then be defined like this:
</para>
<sconstruct>
Java('classes', 'src', JAVAVERSION='1.6')
</sconstruct>
<para>
See <varname>JAVAVERSION</varname> in the man page for more information.
</para>
</section>
<section>
<title>Building Java Archive (<filename>.jar</filename>) Files: the &b-Jar; Builder</title>
<para>
After building the class files,
it's common to collect them into
a Java archive (<filename>.jar</filename>) file,
which you do by calling the &b-link-Jar; Builder method.
If you want to just collect all of the
class files within a subdirectory,
you can just specify that subdirectory
as the &b-Jar; source:
</para>
<scons_example name="jar1">
<file name="SConstruct" printme="1">
Java(target = 'classes', source = 'src')
Jar(target = 'test.jar', source = 'classes')
</file>
<file name="src/Example1.java">
public class Example1
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="src/Example2.java">
public class Example2
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="src/Example3.java">
public class Example3
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
</scons_example>
<para>
&SCons; will then pass that directory
to the &jar; command,
which will collect all of the underlying
<filename>.class</filename> files:
</para>
<scons_output example="jar1">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
<para>
If you want to keep all of the
<filename>.class</filename> files
for multiple programs in one location,
and only archive some of them in
each <filename>.jar</filename> file,
you can pass the &b-Jar; builder a
list of files as its source.
It's extremely simple to create multiple
<filename>.jar</filename> files this way,
using the lists of target class files created
by calls to the &b-link-Java; builder
as sources to the various &b-Jar; calls:
</para>
<scons_example name="jar2">
<file name="SConstruct" printme="1">
prog1_class_files = Java(target = 'classes', source = 'prog1')
prog2_class_files = Java(target = 'classes', source = 'prog2')
Jar(target = 'prog1.jar', source = prog1_class_files)
Jar(target = 'prog2.jar', source = prog2_class_files)
</file>
<file name="prog1/Example1.java">
public class Example1
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="prog1/Example2.java">
public class Example2
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="prog2/Example3.java">
public class Example3
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
<file name="prog2/Example4.java">
public class Example4
{
public static void main(String[] args)
{
System.out.println("Hello Java world!\n");
}
}
</file>
</scons_example>
<para>
This will then create
<filename>prog1.jar</filename>
and <filename>prog2.jar</filename>
next to the subdirectories
that contain their <filename>.java</filename> files:
</para>
<scons_output example="jar2">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
</section>
<section>
<title>Building C Header and Stub Files: the &b-JavaH; Builder</title>
<para>
You can generate C header and source files
for implementing native methods,
by using the &b-link-JavaH; Builder.
There are several ways of using the &JavaH Builder.
One typical invocation might look like:
</para>
<scons_example name="javah">
<file name="SConstruct" printme="1">
classes = Java(target = 'classes', source = 'src/pkg/sub')
JavaH(target = 'native', source = classes)
</file>
<file name="src/pkg/sub/Example1.java">
package pkg.sub;
public class Example1
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example2.java">
package pkg.sub;
public class Example2
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example3.java">
package pkg.sub;
public class Example3
{
public static void main(String[] args)
{
}
}
</file>
</scons_example>
<para>
The source is a list of class files generated by the
call to the &b-link-Java; Builder,
and the target is the output directory in
which we want the C header files placed.
The target
gets converted into the <option>-d</option>
when &SCons; runs &javah;:
</para>
<scons_output example="javah">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
<para>
In this case,
the call to &javah;
will generate the header files
<filename>native/pkg_sub_Example1.h</filename>,
<filename>native/pkg_sub_Example2.h</filename>
and
<filename>native/pkg_sub_Example3.h</filename>.
Notice that &SCons; remembered that the class
files were generated with a target directory of
<filename>classes</filename>,
and that it then specified that target directory
as the <option>-classpath</option> option
to the call to &javah;.
</para>
<para>
Although it's more convenient to use
the list of class files returned by
the &b-Java; Builder
as the source of a call to the &b-JavaH; Builder,
you <emphasis>can</emphasis>
specify the list of class files
by hand, if you prefer.
If you do,
you need to set the
&cv-link-JAVACLASSDIR; construction variable
when calling &b-JavaH;:
</para>
<scons_example name="JAVACLASSDIR">
<file name="SConstruct" printme="1">
Java(target = 'classes', source = 'src/pkg/sub')
class_file_list = ['classes/pkg/sub/Example1.class',
'classes/pkg/sub/Example2.class',
'classes/pkg/sub/Example3.class']
JavaH(target = 'native', source = class_file_list, JAVACLASSDIR = 'classes')
</file>
<file name="src/pkg/sub/Example1.java">
package pkg.sub;
public class Example1
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example2.java">
package pkg.sub;
public class Example2
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example3.java">
package pkg.sub;
public class Example3
{
public static void main(String[] args)
{
}
}
</file>
</scons_example>
<para>
The &cv-JAVACLASSDIR; value then
gets converted into the <option>-classpath</option>
when &SCons; runs &javah;:
</para>
<scons_output example="JAVACLASSDIR">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
<para>
Lastly, if you don't want a separate header file
generated for each source file,
you can specify an explicit File Node
as the target of the &b-JavaH; Builder:
</para>
<scons_example name="javah_file">
<file name="SConstruct" printme="1">
classes = Java(target = 'classes', source = 'src/pkg/sub')
JavaH(target = File('native.h'), source = classes)
</file>
<file name="src/pkg/sub/Example1.java">
package pkg.sub;
public class Example1
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example2.java">
package pkg.sub;
public class Example2
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example3.java">
package pkg.sub;
public class Example3
{
public static void main(String[] args)
{
}
}
</file>
</scons_example>
<para>
Because &SCons; assumes by default
that the target of the &b-JavaH; builder is a directory,
you need to use the &File; function
to make sure that &SCons; doesn't
create a directory named <filename>native.h</filename>.
When a file is used, though,
&SCons; correctly converts the file name
into the &javah; <option>-o</option> option:
</para>
<scons_output example="javah_file">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
</section>
<section>
<title>Building RMI Stub and Skeleton Class Files: the &b-RMIC; Builder</title>
<para>
You can generate Remote Method Invocation stubs
by using the &b-link-RMIC; Builder.
The source is a list of directories,
typically returned by a call to the &b-link-Java; Builder,
and the target is an output directory
where the <filename>_Stub.class</filename>
and <filename>_Skel.class</filename> files will
be placed:
</para>
<scons_example name="RMIC">
<file name="SConstruct" printme="1">
classes = Java(target = 'classes', source = 'src/pkg/sub')
RMIC(target = 'outdir', source = classes)
</file>
<file name="src/pkg/sub/Example1.java">
package pkg.sub;
public class Example1
{
public static void main(String[] args)
{
}
}
</file>
<file name="src/pkg/sub/Example2.java">
package pkg.sub;
public class Example2
{
public static void main(String[] args)
{
}
}
</file>
</scons_example>
<para>
As it did with the &b-link-JavaH; Builder,
&SCons; remembers the class directory
and passes it as the <option>-classpath</option> option
to &rmic:
</para>
<scons_output example="RMIC">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
<para>
This example would generate the files
<filename>outdir/pkg/sub/Example1_Skel.class</filename>,
<filename>outdir/pkg/sub/Example1_Stub.class</filename>,
<filename>outdir/pkg/sub/Example2_Skel.class</filename> and
<filename>outdir/pkg/sub/Example2_Stub.class</filename>.
</para>
</section>
|