Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile packages in java?

Tags:

I have written a simple package program:


  //A simple package         package MyPack          class Balance        {         String name;         double bal;          Balance(String n, double b)         {             name=n;                     bal=b;             }          void show()             {             if(bal<0)             System.out.println("-->");             System.out.println(name+ ": $:" +bal);         }         }          class AccountBalance         {             public static void main(String args[])             {                        Balance current[]=new Balance[3];                     current[0]=new Balance("A.K.Juwatkar",123.123);                     current[1]=new Balance("A.P.Dhoke",345.67);                     current[2]=new Balance("Anil Sarang",100.98);                      for(int i=0;i<3;i++)                 current[i].show();                }         } 

I am using Ubuntu 10.04 & When i compile it using

java MyPack.AccountBalance 

I get the following message:

Exception in thread "main" java.lang.NoClassDefFoundError: MyPack/AccountBalance Caused by: java.lang.ClassNotFoundException: MyPack.AccountBalance     at java.net.URLClassLoader$1.run(URLClassLoader.java:217)     at java.security.AccessController.doPrivileged(Native Method)     at java.net.URLClassLoader.findClass(URLClassLoader.java:205)     at java.lang.ClassLoader.loadClass(ClassLoader.java:321)     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)     at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: MyPack.AccountBalance. Program will exit. 

What is wrong? Please help me out. I have installed openjdk, do i need to install anything else?? I am using Ubuntu 10.04, kindly help me out

like image 439
user1388626 Avatar asked May 11 '12 06:05

user1388626


People also ask

What is the command to compile the Java package?

The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

How do I run a Java package program?

How to run java package program. You need to use fully qualified name e.g. mypack. Simple etc to run the class. The -d is a switch that tells the compiler where to put the class file i.e. it represents destination.

How do I run a Java package in Terminal?

It is a good practice to use names of packages with lower case letters to avoid any conflicts with the names of classes and interfaces. Now a package/folder with the name animals will be created in the current directory and these class files will be placed in it as shown below. and get the result as shown below.


2 Answers

Best is to compile and run the classes from outside the packages :

First you compile with javac :

$javac MyPack/AccountBalance.java 

this will create a new file in the MyPack folder called AccountBalance.class

then you can run it :

$java MyPack.AccountBalance 

By the way: it is discouraged to have package names start with a capital.

like image 181
Grims Avatar answered Nov 21 '22 23:11

Grims


When you are trying to compile the java class, use the '-d' option (destination) to specify where the .class files should be located.

javac -d "classes" AccountBalance.java 

and when you run your program, make sure that same folder is included in your class path:

java -classpath "classes" MyPack.AccountBalance 
like image 26
shayan ray Avatar answered Nov 21 '22 23:11

shayan ray