What exactly is a class path in java?

断了今生、忘了曾经 提交于 2019-11-28 11:02:32

Try that:

java -classpath "$CLASSPATH:nameOfFile.jar:lib/*" path.to.your.MainClass

What this does is setting the classpath to the value of $CLASSPATH, plus nameOfFile.jar, plus all the .jar files in lib/.

Chinmay Patel

CLASSPATH is an environment variable that helps us to educate the Java Virtual Machine from where it will start searching for .class files.

We should store the root of the package hierarchies in the CLASSPATH environment variables.

In case of adding or using jar libraries in our project, we should put the location of the jar file in the CLASSPATH environment variable.

Example: If we are using jdbc mysql jar file in our java project, We have to update the location of the mysql jar file in the CLASSPATH environment variable. if our mysql.jar is in c:\driver\mysql.jar then

We can set the classpath through DOS in Windows

set CLASSPATH=%CLASSPATH%;c:\driver\mysql.jar 

In Linux we can do

export CLASSPATH=$CLASSPATH:[path of the jar]

Hope it helps!

You need to set class path using

The below works in bash . This is temporary

set CLASSPATH=$CLASSPATH=[put the path here for lib]

If you want it permanent then you can add above lines in ~/.bashrc file

export CLASSPATH=$CLASSPATH:[put the path here for lib]:.

When you use a META-INF/MANIFEST.MF file to specify the Main-Class dependencies must be specified in the manifest too.

The -jar switch ignores all other classpath information - see the tools docs for more.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!