以不同形式从cli指定java classpath [英] Specifying java classpath from cli in different forms

查看:157
本文介绍了以不同形式从cli指定java classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过很多痛苦之后,我可以从 CodeNarc jar中运行命令行如下:

  $ ls -1 lib 
CodeNarc-0.25.2.jar
groovy-all-2.4.6.jar
log4j.properties
log4j-1.2.17.jar

$ cd lib
$ java -classpath Groovy的全2.4.6.jar; CodeNarc-0.25.2.jar; log4j的-1.2.17.jar ;. org.codenarc.CodeNarc

但是,如果我退出lib目录本身,而是尝试

  $ java -classpathliborg.codenarc.CodeNarc $ b通过类路径指定它,它无法找到main: $ b错误:无法找到或加载主类org.codenarc.CodeNarc 

我尝试了很多包括完整路径,相对路径,CWD的变体,只指定CodeNarc jar和lib目录(即:CodeNarc-0.25.2.jar; lib)以及其他变体。如何在不指定jar文件中指定每个jar依赖项的情况下运行此jar:

  $ java -classpathliborg.codenarc.CodeNarc 

这个班似乎没有什么不同之处。 CodeNarc类里有一个静态主要方法。



注意:这是在在Windows 10上使用MINGW64环境,因此;将jar分隔为:。

解决方案

创建一个.bat文件在要设置为CLASSPATH的目录中,并在文件中写入以下内容,然后运行该文件。

  set CLASSPATH =% 〜dp0 

您甚至可以使用以下命令运行java程序。 java是运行你的主类(带有main方法的类)的命令,InitArray是我的.class文件的名称。你的可以是不同的。 5 0 4是主要的方法参数。你的java程序可能不会使用主要的方法参数。 暂停只是为了防止控制台关闭,然后才能看到发生了什么。

  set CLASSPATH =%〜dp0 
java InitArray 5 0 4
pause


After much pain, I've been able to get the CodeNarc jar running from the command line as follows:

$ ls -1 lib
  CodeNarc-0.25.2.jar
  groovy-all-2.4.6.jar
  log4j.properties
  log4j-1.2.17.jar

$ cd lib
$ java -classpath "groovy-all-2.4.6.jar;CodeNarc-0.25.2.jar;log4j-1.2.17.jar;." org.codenarc.CodeNarc

However, if I step out of the lib directory itself, and instead try to specify it through the classpath, it fails to find main:

$ java -classpath "lib" org.codenarc.CodeNarc
  Error: Could not find or load main class org.codenarc.CodeNarc

I've tried many variations of this, including the full path, relative paths, CWD, specifying just the CodeNarc jar plus the lib directory (ie: "CodeNarc-0.25.2.jar;lib) among other variations. The only way I've been able to get it running is with the exact jar names given.

How can I run this jar without specifying every jar dependency in -classpath?:

$ java -classpath "lib" org.codenarc.CodeNarc

There doesn't appear to be anything unusual with the class. The CodeNarc class has a static main method inside of it.

Note: this is running within a MINGW64 environment on Windows 10, hence the ";" separating the jars instead of ":".

解决方案

Create a .bat file in the directory you want to set as CLASSPATH and write the following in the file and then run the file.

set CLASSPATH = %~dp0

You can even run the java program using the following. "java" is the command that runs your main class(class with the main method), "InitArray" is the name of my .class file. Yours can be different. "5 0 4" are main method arguments. Your java program might not be using main method arguments. "pause" is just there to prevent console from closing before you can see what happened.

set CLASSPATH = %~dp0
java InitArray 5 0 4
pause

这篇关于以不同形式从cli指定java classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆