user2660852
user2660852

Reputation:

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

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..?

Upvotes: 2

Views: 14249

Answers (3)

Sid
Sid

Reputation: 713

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

Upvotes: 1

Angular University
Angular University

Reputation: 43087

Try this from the directory D:\myProjects:

java -cp D:\library\gson-2.2.4.jar new_example.MyClass

Upvotes: 0

Krzysztof Bogdan
Krzysztof Bogdan

Reputation: 963

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

Upvotes: 1

Related Questions