build java to jar,在Windows命令行中将Jar文件添加到Buildpath

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

2 JARS named profiles.jar and

classifier.jar in the root folder

I want to create a "makefile?" or "batch file?" to compile and run these classes FROM THE WINDOWS COMMAND LINE, but first add the jars to the 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. What are the commands I need to use to run this in command prompt?

Thanks

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

Here are the commands

Im running:

set

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

Then:

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

src/Interaction

Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong?

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. 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 e.g.

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

In version 6 you can use wildcards for the jars e.g.

java -cp ./src:* MainClass

but it might be cleaner putting your jars into a sub directory e.g.

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

So basically, your makefile or start script needs to construct a command like one of the above.

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:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值