简体   繁体   English

在Windows命令行中将Jar文件添加到Buildpath

[英]Add Jar File to Buildpath in Windows Command Line

Im quite annoyed at having to ask this but I cant get it to work. 我很不高兴不得不问这个,但我不能让它工作。 Currently I have a project with: 目前我有一个项目:

5 Classes in the src/ folder 5个src /文件夹中的类

2 JARS named profiles.jar and classifier.jar in the root folder 2 JARS在根文件夹中命名为profiles.jar和classifier.jar

I want to create a "makefile?" 我想创建一个“makefile?” or "batch file?" 还是“批处理文件?” to compile and run these classes FROM THE WINDOWS COMMAND LINE , but first add the jars to the buildpath? 从WINDOWS命令行编译和运行这些类,但首先将jar添加到buildpath? Im not sure how I go about this 我不知道我是怎么回事

When I try to do it, it says that the class is not found, most likely due to me not adding the jars to the buildpath correctly. 当我尝试这样做时,它表示找不到类,很可能是因为我没有正确地将jar添加到构建路径中。 What are the commands I need to use to run this in command prompt? 在命令提示符下运行此命令需要使用哪些命令?

Thanks Philip 谢谢Philip

EDIT 编辑

Thanks for the help, Im having alot of trouble getting it to work tho Currently I have a project with 5 classes in the src folder, and 2 jars in the jar folder 感谢您的帮助,我有很多麻烦让它工作当前我有一个项目在src文件夹中有5个类,在jar文件夹中有2个jar

Here are the commands Im running: 以下是我正在运行的命令:

set CLASSPATH=C:\\wamp\\www\\news\\UserProfiling\\jars\\classifier.jar ;C:\\wamp\\www\\news\\UserProfiling\\jars\\profiles.jar 设置CLASSPATH = C:\\ wamp \\ www \\ news \\ UserProfiling \\ jars \\ classifier.jar; C:\\ wamp \\ www \\ news \\ UserProfiling \\ jars \\ profiles.jar

Then from the root folder, Im running: 然后从根文件夹,我运行:

javac src/*.java javac src / * .java

Then: 然后:

java -cp ./src:./jars/* src/Interaction java -cp ./src:./jars/* src / Interaction

Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong? 交互是主要的类,我得到各种noclassfound错误,我做错了什么? Many thanks Philip 非常感谢菲利普

THE ERROR 错误

java -cp ./src:./jars/* Interaction Exception in thread "main" java.lang.NoClassDefFoundError: Interaction Caused by: java.lang.ClassNotFoundException: Interaction 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) at java.lang.ClassLoader.loadClassInternal(Unknown Source) Could not find the main class: Interaction. java -cp ./src:./jars/*交互线程“main”中的异常java.lang.NoClassDefFoundError:交互引起的:java.lang.ClassNotFoundException:java.net.URLClassLoader上的交互$ 1.run(未知来源)at java.security.AccessController.doPrivileged(本机方法),位于java.lang.ClassLoader.findClass(未知来源),位于sun.misc.Launcher的$ java.lang.ClassLoader.loadClass(未知来源)$ AppClassLoader.loadClass(未知来源)at java.lang.ClassLoader.loadClassInternal(Unknown Source)中的java.lang.ClassLoader.loadClass(Unknown Source)无法找到主类:Interaction。 Program will exit. 程序将会退出。

In version older than or equal to Java version 5 you must specify each jar individually, and the root of your source, on your classpath eg 在早于或等于Java版本5的版本中,您必须在类路径上单独指定每个jar和源的根目录,例如

java -cp a.jar:b.jar:c.jar:./src MainClass

In version 6 you can use wildcards for the jars eg 在版本6中,您可以使用通配符作为jar

java -cp ./src:* MainClass

but it might be cleaner putting your jars into a sub directory eg 但将你的罐放入子目录可能更清洁,例如

java -cp ./src:./jars/* MainClass

So basically, your makefile or start script needs to construct a command like one of the above. 所以基本上,你的makefile或者启动脚本需要构建一个像上面那样的命令。

More info - Sun docs (v6) 更多信息 - Sun docs(v6)

Update - in response to your second edit, you need to specify the full main class name, so if the class is in a package called 'com.mypackage.MainClass' then you need to do: 更新 - 为了响应您的第二次编辑,您需要指定完整的主类名,因此如果该类位于名为“com.mypackage.MainClass”的包中,那么您需要执行以下操作:

java -cp ./src:./jars/* com.mypackage.MainClass

I'd also suggest getting the command working as a standalone command first, before getting the whole script running. 我还建议在使整个脚本运行之前,先将命令作为独立命令运行。 By removing moving parts it will be faster to debug and easier to see what's going on. 通过移除移动部件,调试速度更快,更容易看到发生了什么。

I would suggest you take a look at ant or maven . 我建议你看看蚂蚁maven Ant is a solution to do pretty much straightforward what you want to do, maven is not as straightforward but has its advantages when it comes to managing dependencies. Ant是一个非常简单的解决方案,maven不是那么简单,但在管理依赖关系方面有其优势。

About your second question : if you use 关于你的第二个问题:如果你使用

java -cp ./src:./jars/* src/Interaction

it will try to launch the class src/Interaction, which does not exists. 它将尝试启动不存在的类src / Interaction。 src is already in your classpath, so you just have to do src已经在你的类路径中,所以你必须这样做

java -cp ./src:./jars/* Interaction

Found your problem, I think. 我想,找到了你的问题。

javac src/*.java javac src / * .java

java -cp ./src:./jars/* src/Interaction java -cp ./src:./jars/* src / Interaction

This problem plagues many beginners: You need to run javac and java from the directory where your source tree starts. 这个问题困扰了许多初学者:您需要从源树开始的目录运行javac和java。 In your case, that directory is src . 在您的情况下,该目录是src Since src does not appear in your package names, it should also not appear in your compile/execution paths. 由于src没有出现在您的包名中,因此它也不应出现在您的编译/执行路径中。

So you should 所以你应该

cd UserProfiling/src 

and run 并运行

javac *.java 

and

java -cp .:../jars Interaction 

from there. 从那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM