How do to run java class in command Line (cmd)? (use classpath)

14,024

Solution 1

All you need: en.wikipedia.org/wiki/Classpath_(Java)

Solution 2

I would suggest trying this:

java -cp "jar_name.jar;libs/*" com.test.App

jar_name.jar : your jar name with .jar extension

libs/* : relative path to your dependency jars

com.test.App : Class with main(String[]) method
Share:
14,024
tshepang
Author by

tshepang

I do software development for a living and as a hobby. My favorite language is Rust, and I've used Python much in the past. My OS of choice is Debian.

Updated on June 04, 2022

Comments

  • tshepang
    tshepang almost 2 years

    I have java class in folder D:\myProjects\new_example:

    package new_example;
    
    import com.google.gson.Gson;
    
    class MyClass{
    
        public static void main(String[] args) {
            MyClass myClass = new MyClass();
            Gson gson = new Gson();
            System.out.println(gson.toJson(myClass.getMyDate()));
        } 
        public String getMyDate(){
           return "Hello";
        }
     }
    

    How do to run this class in command Line (cmd) from disk D:? (If gson-2.2.4.jar is located: D:\library\gson-2.2.4.jar AND MyClass.java in D:\myProjects\new_example\MyClass.java), use classpath... How do to run it..?