| | 34 | |
|---|
| | 35 | = Compiling java code using gcj = |
|---|
| | 36 | |
|---|
| | 37 | Java source code (.java files) can be compiled using the command 'gcj'. |
|---|
| | 38 | |
|---|
| | 39 | For example: |
|---|
| | 40 | {{{ |
|---|
| | 41 | #!sh |
|---|
| | 42 | gcj HelloWorld.java |
|---|
| | 43 | }}} |
|---|
| | 44 | |
|---|
| | 45 | This example will not work, however, as gcj must be told which main method to run when specifying source files, for example: |
|---|
| | 46 | |
|---|
| | 47 | {{{ |
|---|
| | 48 | #!sh |
|---|
| | 49 | gcj --main=HelloWorld -o HelloWorld HelloWorld.java |
|---|
| | 50 | }}} |
|---|
| | 51 | |
|---|
| | 52 | The parameter "--main=<className>" tells the compiler that the executable should run the main within the !HelloWorld class and the "-o" parameter specifies the output file. |
|---|
| | 53 | |
|---|
| | 54 | If the code is successfully compiled, the executable will be a file named "!HelloWorld in the current working directory. If no output file is specified, the executable will be the file named "a.out" in the current working directory. |
|---|
| | 55 | |
|---|
| | 56 | Run the program from the current working directory with the following command: |
|---|
| | 57 | |
|---|
| | 58 | {{{ |
|---|
| | 59 | #!sh |
|---|
| | 60 | ./HelloWorld |
|---|
| | 61 | }}} |
|---|
| | 62 | |
|---|
| | 63 | To compile to java bytecode (.class file), use the -C option: |
|---|
| | 64 | |
|---|
| | 65 | {{{ |
|---|
| | 66 | #!sh |
|---|
| | 67 | gcj -C HelloWorld.java |
|---|
| | 68 | }}} |
|---|
| | 69 | |
|---|
| | 70 | This command will produce "!HelloWorld.class" which can then be run in the java virtual machine: |
|---|
| | 71 | |
|---|
| | 72 | {{{ |
|---|
| | 73 | #!sh |
|---|
| | 74 | java HelloWorld |
|---|
| | 75 | }}} |
|---|
| | 76 | |
|---|
| | 77 | Notice that you only specify the class name, not the bytecode file name. |
|---|