0

just need sum help for classpath settings for command line compiling of classes of new java packages. i've been trying to do it but it only works for one package.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

Follow these steps:

package mylib;

class First{
   public static void main(String []args) {
     System.out.println("First");
   }
}
public class Sample{
   public static void main(String []args){
     System.out.println("Sample");
   }
}

Now, compile the Sample.java file with following command line options;
(javac SourceFile.java –d [directoryname-where –you-want-to-create-package])
>javac Sample.java –d .
Now, execute the java bytescode using following command line
>java mylib.First
>java mylib.Sample

Another Sample:
Access the public classes of packages

Step-1. Create Sample.java file at c:\test1 folder and compile it

package mylib;
public class Sample{
  public Sample(){
   System.out.println("Sample from mylib");
  }
}

Step-2 Create Example.java file at c:\test2 folder and compile it.

package yourlib;
public class Example{
   public Example() {
     System.out.println("Example");
   }
}

Step-3. Create another .java file through which you want to access the classes of two packages.

public class TwoPackage{
   public static void main(String []args) {
       mylib.Sample a=new mylib.Sample();
       yourlib.Example b=new yourlib.Example();
   }
}

OR

import mylib.Sample;     
import yourlib.Example;  

public class TwoPackage{
   public static void main(String []args) {
       Sample a=new Sample();
       Example b=new Example();
   }
}

To compile TwoPackage.java file,
>javac TwoPackage.java –cp .;”%classpath%”;c:\test1;c:\test2;
To Execute bytescode,
>java –cp .;”%classpath%”;c:\test1;c:\test2; TwoPackage

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.