Q:

How to create a jar. Use it in a class. execute the class using the jar in command line?

I am trying to solve a problem in Parts.
This is my first part
I need to create a JAR, call that JAR from a java class and execute it in command line.
Here is what i have done.
Directory structure:
root->com->test->sample->Sample1.java
Sample1.java is this:
        package com.test.sample;
        public class Sample1 {

        // --- Native methods
        //public native boolean booleanMethod(boolean bool);

        // --- Main method to test our native library
        public void Sample1() {

        }

        public void callingBool() {
            //System.loadLibrary("Sample1");
            //boolean bool = this.booleanMethod(true);
            //System.out.println("booleanMethod: returned" + bool);
            System.out.println("booleanMethod: Called without loading 
sample");
            }
        }

I create a Jar out of this going to root directory. Use command 
    jar cvf asd.jar com

    Output:
    added manifest
    adding: com/(in = 0) (out= 0)(stored 0%)
    adding: com/test/(in = 0) (out= 0)(stored 0%)
    adding: com/test/sample/(in = 0) (out= 0)(stored 0%)
    adding: com/test/sample/Sample1.java(in = 494) (out= 236) 
    (deflated 52%)

    jar -tf asd.jar

    META-INF/
    META-INF/MANIFEST.MF
    com/
    com/test/
    com/test/sample/
    com/test/sample/Sample1.java

So the JAR is now created. I copy paste the JAR into another directory "jaronly"
Add another Java file Sample1Caller into /jaronly which is :
    import  com.test.sample.Sample1;

    public class Sample1Caller {

        // --- Native methods
        //public native boolean booleanMethod(boolean bool);

            // --- Main method to test our native library
        public static void main(String[] args) {
            //System.loadLibrary("Sample1");
            Sample1 sample = new Sample1();
            sample.callingBool();
            System.out.println("Callled Sample1 now" );
        }
    }

After this i compile Sample1Caller.java in /jaronly
javac -cp .:./asd.jar Sample1Caller.java

This compiles and produces 2 class files Sample1Caller.class and Sample1.class
I am not sure if Sample1.class should be created (i am referring to it from asd.jar already)
When i execute Sample1Caller using the below command i get following error
    java -cp .:./asd.jar Sample1Caller

    Exception in thread "main" java.lang.NoClassDefFoundError: 
    com/test/sample/Sample1
    at Sample1Caller.main(Sample1Caller.java:12)
    Caused by: java.lang.ClassNotFoundException: com.test.sample.Sample1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.lo
    Class(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

It compiles but does not execute. Is there something wrong with my execution command or generating the .jar file or compiling the jar with java. 
PS: I hardly have raised any questions here before so pardon my formatting.

A:

JAR files should contain classes and not sources. So before you make asd.jar, you should compile Sample1.java. This line would do the trick
javac com/test/sample/*.java

But then Sample1.java will still be in the JAR and you don't want it there.
To put more structure to your build, I suggest you create the following folder structure:
 sample_project
 sample_project/src
 sample_project/src/com/test/sample/Sample1.java
 sample_project/target
 sample_project/build.bat

Then the contents of build.bat will be:
:: compile
cd src
javac com/test/sample/*.java -d ../target

:: create jar
cd ../target
jar cvf asd.jar com

:: finish
cd ..

