来自java命令行的maven依赖插件的classpath文件

时间:2014-06-16 14:52:11

标签: java maven batch-file command-line

我想获取项目的maven dependency,并将其用作classpath的命令行项目(Windows批处理)。

这是我根据batch文件中的maven dependency plugin所做的。

call mvn dependency:build-classpath -Dmdep.outputFile=test.txt
java -cp `cat test.txt` com.hqly.main.Hqly

cat命令未执行且classpath未正确设置。它没有看到我的main课程。 我在尝试execute

时遇到以下错误
[INFO] --- maven-dependency-plugin:2.1:build-classpath (default-cli) @ hqly ---
[INFO] Skipped writing classpath file 'C:\Users\chandrans1\Desktop\GitHub\hqly\test.txt'.  No changes found.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.926s
[INFO] Finished at: Mon Jun 16 15:44:39 BST 2014
[INFO] Final Memory: 8M/109M
[INFO] ------------------------------------------------------------------------
Exception in thread "main" java.lang.NoClassDefFoundError: test/txt`
Caused by: java.lang.ClassNotFoundException: test.txt`
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: test.txt`.  Program will exit.

1 个答案:

答案 0 :(得分:1)

与Linux不同,Windows不使用反向标记作为将命令结果作为程序参数传递的方法。

您可以查看此问题,了解有关如何在Windows中解决此限制的一些想法:Batch equivalent of Bash backticks。这样的事情应该有效:

for /f %%i in (test.txt) do set HSQLY_CLASSPATH=%%i
echo Classpath is "%HSQLY_CLASSPATH%" 
java -cp %HSQLY_CLASSPATH% com.hqly.main.Hqly