Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set classpath for current directory in Java? [duplicate]

Tags:

java

How can I include current folder as classpath? I got a Utility and a Main class like;

/home/project/Main.class
/home/project/libs/com/fr/Utility.class

When I try to run Main class as(under /home/project/ dir);

java -cp "libs/*;" Main

I am getting below error.

Error: Could not find or load main class Main

EDIT:

Main class;

import com.fr.Utility;

public class Main{
 ....
}

Utility class;

package com.fr;

public class Utility{
 ....
}

When I run;

java -cp .:libs/* Main

I am getting below error;

Exception in thread "main" java.lang.NoClassDefFoundError: com/fr/Utility
Caused by: java.lang.ClassNotFoundException: com.fr.Utility
like image 992
hellzone Avatar asked Sep 09 '26 05:09

hellzone


1 Answers

You should use colon as the path separator : if you are on Linux, ; if on Windows.

Also include the current path with a dot . and remove the wildcard * from the classpath:

java -cp .:libs/ Main or java -cp .;libs/ Main

See this answer and also the "Understanding class path wildcards" section in this documentation.

like image 82
am9417 Avatar answered Sep 10 '26 18:09

am9417



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!