I have download a simple java example from the net.I am trying to compile the code given below
package ArrayList;
import java.util.ArrayList;
public class SimpleArrayListExample {
public static void main(String[] args) {
//create an ArrayList object
ArrayList<String> arrayList = new ArrayList<String>();
/*
Add elements to Arraylist using
boolean add(Object o) method. It returns true as a general behavior
of Collection.add method. The specified object is appended at the end
of the ArrayList.
*/
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
/*
Use get method of Java ArrayList class to display elements of ArrayList.
Object get(int index) returns and element at the specified index in
the ArrayList
*/
System.out.println("Getting elements of ArrayList");
System.out.println(arrayList.get(0));
System.out.println(arrayList.get(1));
System.out.println(arrayList.get(2));
}
}
I have edited the program as per your suggestion and I can compile and I got the class file.
java SimpleArrayListExample.java
NOw I am trying to execute the file using
java -classpath . ArrayList.SimpleArrayListExample.java
Exception in thread “main” java.lang.NoClassDefFoundError
I google and found out that I have to specify -classpath .
http://www.tech-recipes.com/rx/826/java-exception-in-thread-main-javalangnoclassdeffounderror/ that doesn’t seem to fix the problem.
The compiler is warning that the type contained within the collection has not been declared.
You can use generics to emilinate this warning
Edit:
There is a problem with your package structure. The
SimpleArrayListExampleis not in a directory calledArrayList. To fixSimpleArrayListExample.javato a new directory calledArrayList.javac ArrayList/SimpleArrayListExample.javajava ArrayList.SimpleArrayListExampleNote package names typically use lowercase.