ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants...

31
Using Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code (one file) in Linux so many of you are used to an IDE that takes care of everything! compiling and running Java programs does NOT require and IDE on IDE on GL, just an editor (EMACS, Vim, etc… ) base commands are javac (compile) and java (run) there are basic steps o compile each file o “run” the file that has the driver compiling will create *.class files o this can get messy if you have a lot of files Compiling a simple class File public class Hello { public static void main(String args[]) { System.out.print("Hello Class, "); System.out.println("I am Mr. Lupoli"); // what is the difference between System.out.println("We will learn JAVA!!"); // 1

Transcript of ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants...

Page 1: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Using Linux & Ants w/ Java

(need to add pics to some spots done in flipped)Compiling simple code (one file) in Linux

so many of you are used to an IDE that takes care of everything! compiling and running Java programs does NOT require and IDE on IDE on GL, just an editor (EMACS, Vim, etc… ) base commands are javac (compile) and java (run) there are basic steps

o compile each fileo “run” the file that has the driver

compiling will create *.class fileso this can get messy if you have a lot of files

Compiling a simple classFilepublic class Hello{ public static void main(String args[]) { System.out.print("Hello Class, "); System.out.println("I am Mr. Lupoli"); // what is the difference between System.out.println("We will learn JAVA!!"); // println and print?? }}Linux Procedure[slupoli@linux3 simpleClass]$ emacs Hello.java[slupoli@linux3 simpleClass]$ javac Hello.java[slupoli@linux3 simpleClass]$ java HelloHello Class, I am Mr. LupoliWe will learn JAVA!!After effects

1

Page 2: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Hello.class Hello.javaNotice, not java Hello.java (or .class)Compiling complex setups (multiple classes) in Linux

while java and javac are still used, but now we have more to compile basic steps

o compile all files, start with driver since it depends on everyone else because of this, all of the other files instantiated will be compiled

o run driver file

Compiling a complex setup of classesFile(s)

Linux Procedure(after creating each file)[slupoli@linux3 simpleClass]$ javac Driver.java[slupoli@linux3 simpleClass]$ java DriverDog [bark=null, name=Eric, sound=null, food=null, weight=0.0]Employee [firstname=Peter, lastname=Joyce, age=60]************ SORTING ***********Employee [firstname=Peter, lastname=Joyce, age=60]Employee [firstname=Prof. L, lastname=Lupoli, age=30]Employee [firstname=Jack, lastname=McLaughlin, age=90]************ SORTING - Part 2 ***********Cat [bark=null, name=Victoria, sound=null, food=null, weight=17.0]Cat [bark=null, name=Porche, sound=null, food=null, weight=23.0]Dog [bark=null, name=Amy, sound=null, food=null, weight=110.0]Dog [bark=null, name=Eric, sound=null, food=null, weight=225.0]After effectsAnimal.class Cat.java Dog.class Driver.java Sort.class

2

Page 3: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Animal.java Cells.class Dog.java Employee.class Sort.javaCat.class Cells.java Driver.class Employee.javaCommand Line Arguments In Theory

program can get values from the command line o instead of user

used in many programming languages but use your header to tell us how to use the command line arguments you

programmed!!! number of arguments is only limited by YOUR programming notice it is AFTER the first token (command)

Example (non-Java) command line arguments

nano project2.c –cls –l

// notice nano nor ls are considered arguments

Example command line arguments

java zipcodes.py CA DEjava calculate.py data.txt

`How command line arguments are handled in Java

all arguments are placed into a list separately (String[] args)o args[0] = "zipcodes.py"o args[1] = "CA"o args[2] = "DE"o WATCH, numeric values are strings at first!!

must convert!! you can use the list properties to your advantage!!

o for(int i = 0; i < args.length; i++) # to discover all arguments

3

Page 4: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Accepting command line valuesDriver that accepts command line valuespublic class Driver { public static void main(String[] args) {

// command line values

// display the array 'args' for (String s: args) { System.out.println(s); }

// display individual values within args for(int i = 0; i < args.length; i++) { System.out.println(args[i]); } // notice I used length above so not to access an index // that DOES NOT contain a value

Example command line entryjava Driver value1 value2java Driver "Lupoli stinks" vacation // still two argsjava Driver value1 value2 23 // 23 would be a String

4

Page 5: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

ANT Background A nother Neat build Tool much like a “make” tool in C/C++ things ANT can do

o helps build and deploy a Java projecto compilingo cleaningo making directorieso making Javadocso deploying .jar files

uses an XML file to complete each “task”o build.xml

can type “ant” in the command prompt and will use an XML file to build, compile and run the projecto make sure your current directory is within the base project directory

not inside sourceo make sure build.xml (the ANT file) is present!!

to make it easier on you (and me) it’s best to use packages!! o many examples online use packages

the ANT build file should be EXACTLY the same in both Eclipse/Linux

ANT file and Project Layout (cleaned)

[slupoli@linux3 complexClassANT]$ lsbuild.xml src

5

Page 6: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

What an ANT XML Build file can do But I want you to see the after effects Using Employee/Dog, Cat, Animal Example

What can that Build file doProject BEFORE ANT Project AFTER ANT

Notice this project did NOT use packages

6

Page 7: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

The Java project setup Again, this MUST be consistent in BOTH Eclipse/Linux uses packages to store ALL .java files please note where the compile and generated files are stored after the ant build

Linux Project Directory SetupSetup before ANT Setup after ANT

|-src |---math (MyMath.java inside) |---test (Main.java inside)build.xml

|-bin |---math |---test |-dist |-docs |---math |---resources |---test |-src |---math |---testbuild.xml

Java ANT on Linux – General Directory Setup again the overall structure must follow above

o build.xml in base directoryo all code in a deeper directory “src”

once ANT is operational several more directories appearo bin

.class fileso doc

API (html) fileo dist

jar files

7

Page 8: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Java ANT on Linux – ANT File the build.xml is based on three pieces

o tasks some work like compiling usually small, logical steps

o targets specify dependency or order tasks can be completed in a particular order

o extensionso properties

ANT has several key words (depending on the file setup)o runo cleano compileo docso jaro etc…

8

Page 9: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

ANT in your Eclipse/Linux Project<?xml version="1.0"?><project name="Ant-Test" default="main" basedir="."> <!-- Sets variables which can later be used. --> <!-- The value of a property is accessed via ${} --> <property name="src.dir" location="src" /> <property name="build.dir" location="bin" /> <property name="dist.dir" location="dist" /> <property name="docs.dir" location="docs" />

<!-- Deletes the existing build, docs and dist directory--> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${docs.dir}" /> <delete dir="${dist.dir}" /> </target>

<!-- Creates the build, docs and dist directory--> <target name="makedir"> <mkdir dir="${build.dir}" /> <mkdir dir="${docs.dir}" /> <mkdir dir="${dist.dir}" /> </target>

<!-- Compiles the java code (including the usage of library for JUnit --> <target name="compile" depends="clean, makedir"> <javac srcdir="${src.dir}" destdir="${build.dir}"> </javac> </target>

<!-- Creates Javadoc --> <target name="docs" depends="compile"> <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}"> <!-- Define which files / directory should get included, we include all --> <fileset dir="${src.dir}"> <include name="**" /> </fileset> </javadoc> </target>

<!--Creates the deployable jar file --> <target name="jar" depends="compile"> <jar destfile="${dist.dir}\test.jar" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="Driver" /> </manifest> </jar> </target>

<!-- Runs the jar file --> <target name="run" depends="compile, jar, docs"> <description>run the program</description> <java jar="${dist.dir}/test.jar" fork="true"> <!-- if you needed arguments --> <!-- <arg value="${args0}" /> <arg value="${args1}" /> --> </java> </target>

<target name="main" depends="compile, jar, docs, run"> <description>Main target</description> </target></project>

9

Page 10: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Showing what ANT (build.xml) can do ant

o runs everything clean

o cleans up direcorties and fileso this should be done before you submit

ant run o run the Java program

ant run with parameters ant doc

o generates the API files ant compile

o duh ant jar

o generates the jar file

Eclipse – Creating an ANT file Setup your name java files and packages first Then add another file to your project

o File New Other XML XML File (< Kepler Version)o File New File (>= Kepler Version)o Name it build.xmlo Finish

Should now see the file show up in your Projecto Should have an ant symbol beside it

Eclipse – Running an ANT file I personally had to right click on the ANT build file in the project, and select

“Run As” Ant Build It did take a minute

o Bottom right “bar” shows the progresso I also had to run it twice the first time before I saw anything show up in

the console window On the Project Explorer side, select Refresh to see the new directories

10

Page 11: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Running ANT on EclipseBuildfile: C:\Users\Luper\Desktop\workspace\ANTExample\build.xmlclean: [delete] Deleting directory C:\Users\Luper\Desktop\workspace\ANTExample\bin [delete] Deleting directory C:\Users\Luper\Desktop\workspace\ANTExample\docs [delete] Deleting directory C:\Users\Luper\Desktop\workspace\ANTExample\distmakedir: [mkdir] Created dir: C:\Users\Luper\Desktop\workspace\ANTExample\bin [mkdir] Created dir: C:\Users\Luper\Desktop\workspace\ANTExample\docs [mkdir] Created dir: C:\Users\Luper\Desktop\workspace\ANTExample\distcompile: [javac] C:\Users\Luper\Desktop\workspace\ANTExample\build.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to C:\Users\Luper\Desktop\workspace\ANTExample\binjar: [jar] Building jar: C:\Users\Luper\Desktop\workspace\ANTExample\dist\test.jardocs: [javadoc] Generating Javadoc [javadoc] Javadoc execution [javadoc] Loading source file C:\Users\Luper\Desktop\workspace\ANTExample\src\math\MyMath.java... [javadoc] Loading source file C:\Users\Luper\Desktop\workspace\ANTExample\src\test\Main.java... [javadoc] Constructing Javadoc information... [javadoc] Standard Doclet version 1.7.0_04 [javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes...run: [java] Result is: 50main:BUILD SUCCESSFULTotal time: 11 seconds

What can that Build file do (properly)Project BEFORE ANT Project AFTER ANT

(had to hit refresh)

11

Page 12: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Complete Eclipse example with ANT the ANT file above works with a scenario where there is only one package otherwise you will have to edit the ANT file given

Eclipse Project Setup

package linkedlist;

public class Driver{

public static void main(…){Linked_List one = new

Linked_List();

package linkedlist;public class Linked_List{

package linkedlist;public class NODE{

Transferring from Eclipse to Unix (GL)1. Need to zip up the Eclipse project2. Create directory to drop the files in GL3. Use WinSCP to transfer the files to GL4. unzip project in GL5. run ANT on GL (shown below)

12

Page 13: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Linux – Creating the ANT file (and project setup) create the base directory your code will be in, and src inside that

o only need src directory with the packages inside (and .java files within) create the file use whatever editor you wish

o make sure you name it build.xml

Source File Setuppackage math;

public class MyMath { public int multi(int number1, int number2) { return number1 * number2; }}

package test;

import math.MyMath;

public class Main { public static void main(String[] args) { MyMath math = new MyMath(); System.out.println("Result is: " + math.multi(5, 10)); }}

1. Create a CMSC331/ant/ directory 2. Move inside that directory3. From within, copy the WHOLE structure and all code above with:

cp –r /afs/umbc.edu/users /s/l/slupoli/pub/labCode331/ant/* .4. Explore the different directories, notice build.xml for you to copy5. But build.xml is wrong!!!

a. look at Main-Class’s value. Driver is not correct. Hint: it’s about the packages.

13

Page 14: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Import Zipped project (with ANT) into Eclipse Project WORKED in Linux and has a build XML file Eclipse isn’t as smart as we would like Have to create a project in Eclipse first

o Threads2Ex2 in the example then import the entire project you want

Importing as Usual

notice it did not match up my src directorieso so move them into the right spoto shown below

14

Page 15: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Cleaning the project up

move all directories and files from wrong src to outer src move build.xml to Project directory delete bad project directory

Continuing to clean up the Projectmove #1 move #2 delete bad

the last part is making sure the project knows the source folder has materialo right click on the projecto select propertieso select Java Build Patho select sourceo source fold SHOULD be there.

Otherwise “add folder”

15

Page 16: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Java Build Path setup

16

Page 17: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Using command line values and ANT Your Driver must be able to accept the values

o covered previously but the ANT build file requires some minor amendments

o but ONLY in the “run” portiono can add as many values you need

only 2 shown here <arg value= > target syntax

o constant, cannot changeo ${args0}

value inside {}s can be any variable name you want

The relation between build.xml & command linebuild.xml <target name="run" depends="compile, jar, docs"> <description>run the program</description> <java jar="${dist.dir}/test.jar" fork="true"> <!-- if you needed arguments --> <arg value="${args0}" /> <arg value="${args1}" /> </java> </target>

command lineant run -Dargs0="Lupoli stinks" -Dargs1=vacation

command lineo ant run still works, but must now place the values you want to passo –D

let’s the ANT file know it’s a command line value you are about to enter

o notice the command line variables match the names in the build.xml file

17

Page 18: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

ANT file result with command line arguments[slupoli@linux1 antV4]$ ant run -Dargs0="Lupoli stinks" -Dargs1=vacationBuildfile: build.xml

clean: [delete] Deleting directory /afs/umbc.edu/users/s/l/slupoli/home/CMSC341/antV4/bin [delete] Deleting directory

[javadoc] Building tree for all the packages and classes... [javadoc] Building index for all the packages and classes... [javadoc] Building index for all classes...

run: [java] Lupoli stinks [java] vacation [java] Lupoli stinks [java] vacation [java] Result is: 50

BUILD SUCCESSFULTotal time: 3 seconds

18

Page 19: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Other ANT Issues in Eclipse personally had these issues

Making sure your using the correct JRE/JDK

Making sure ANT is correcly configured

19

Page 20: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

FYI Section

Simple ANT XML file Eclipse does not really help find key XML tags for you thin is script notice how different targets have a dependency

o which makes sense, if you cannot compile then you cannot run includeantruntime='false'

o had to include this to remove an Eclipse error/warning to “Run” we right click on the build XML file in the Project, and select “Run

As” and “Ant Build” The order of “targets” does not matter since dependency helps determine the

order test folder

o will have the compiled .class files from the project

Simple version of Ant’s XML fileXML File<?xml version="1.0" encoding="UTF-8"?><project default='run' name='First Try with Eclipse and Ant'> <target name='compile'> <javac srcdir='./src' includeantruntime='false' destdir='test' />

<!-- </javac> automatically appeared, but all I need is a / at the end of javac tag --> </target> <target name='run' depends='compile'>

</target></project>

Overall Project Setup

20

Page 21: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Ant Javac issue

Buildfile: C:\Users\Luper\Desktop\workspace\AntLinkedList\build.xmlcompile:

BUILD FAILEDC:\Users\Luper\Desktop\workspace\AntLinkedList\build.xml:4: Unable to find a javac compiler;com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does not point to the JDK.It is currently set to "C:\Program Files\Java\jre7"

Should have been set to:

C:\Program Files\Java\jdk1.7.0_04\binorC:\Program Files\Java\jdk1.7.0_04orC:\Program Files\Java\jre7

(reboot after changing)

Option 1

Set the JAVA_HOME VariableOnce you have found the JDK installation path:

1. Right-click the My Computer icon on your desktop and select Properties.2. Click the Advanced System Setting tab. 3. Click the Environment Variables button. 4. Under System Variables, check for a “JAVA_HOME” variable. If there, skip to 6. Otherwise click New.5. Enter the variable name as JAVA_HOME.6. Enter the variable value as the installation path for the Java Development Kit.7. Click OK.8. Click Apply Changes.

You might need to restart windows. (I didn’t)

21

Page 22: ecology labfaculty.cse.tamu.edu/slupoli/notes/Java/JavaAntsNotes.docx · Web viewUsing Linux & Ants w/ Java (need to add pics to some spots done in flipped) Compiling simple code

Option 2 The issue can be within Eclipse Window Preferences Java Installed JREs Make sure it is pointing to the right directory reboot Elcipse

Ant Javadoc issues Had to make sure ANT was configured correctly Also tried downloading the latest JDK

Sources:

Ant in Generalhttp://www.vogella.com/articles/ApacheAnt/article.htmlhttp://www.youtube.com/watch?v=wSpgXhDQDVghttp://www.javaworld.com/jw-10-2000/jw-1020-ant.html

Ant in Eclipsehttp://www.youtube.com/watch?v=ERbyZlyM_d4

22