Java类路径和包问题-引用多个jar文件 [英] Java classpath and package problems - referencing multiple jar files

查看:288
本文介绍了Java类路径和包问题-引用多个jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个Java类,该类引用了其他各种jar文件.看起来像这样:

I have a java class I'm trying to run that references various other jar files. It looks something like this:

 package com.myapp.test;

 import java.io.File;
 import com.myapp.ref;
 import com.myapp.stuff;
 import com.strangersapp.stuff;
 import com.strangersapp.morestuff;

 public class myTest {
    public static void main... 
    ...
 }

com.myapp.*类位于另一个名为myapp.jar的jar文件中. strangersapp类位于strangersapp.jar中.各种jar中还有其他几个引用的类.我的整个目录结构如下:

The com.myapp.* classes are in another jar file called myapp.jar. The strangersapp classes are in a strangersapp.jar. There are several other referenced classes in various jars. My whole directory structure looks like this:

 myapp.jar
 strangersapp.jar
 someother.jar
 yetanother.jar
 etc.jar
 com\myapp\test\myTest.java

我的问题是尝试编译并运行它.我尝试了明显的命令,但是此命令不起作用.它不会拾取所有的jar文件:

My problem is trying to compile and run this. I try the obvious but this command does NOT work. It doesnt pickup all the jar files:

 javac -cp . com\myapp\test\myTest.java
 java -cp . com.myapp.test.myTest

此命令也不起作用:

 javac -cp ".;*.jar" com\myapp\test\myTest.java

但是,这些命令确实有效:

However, these commands do work:

 javac -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com\myapp\test\mTest.java
 java -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com.myapp.test.mTest

有人会知道为什么我的第一个Java运行语句不起作用吗?我不想在类路径参考中输入所有的jar名称...

Would anyone know why my first java run statement is not working?? I dont want to type all the jar names out in the classpath reference...

推荐答案

如果使用-cp,则必须命名要包含的所有jar.为了避免这种情况,只需将它们添加到清单中,java就会选择它们.

If you use -cp you have to name all the jars you want to include. To avoid that just add them to your manifest and java will pick them up.

对于javac,我认为您应该使用-classpath而不是-cp

For javac i think you are supposed to use -classpath rather than -cp

这篇关于Java类路径和包问题-引用多个jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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