Java环境变量设置正确,但是Java找不到jar [英] Java environment variables set correctly, but java is not finding the jars

查看:819
本文介绍了Java环境变量设置正确,但是Java找不到jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

Java环境变量设置正确.但是,只有在javac命令显式包含jar时,才能看到jar.

Java environment variables correctly set, as seen from echo-ing them. However, jars are only seen when explicitly included with javac command.

详细信息

这与图形和OpenGl没有特别的关系,某些问题与类路径有关,即第三方 jar 文件的导入.

This is not specifically related to graphics and OpenGl, certain the problem is related to classpaths, ie the importing of third-party jar files.

使用Windows XP SP3,JDK 1.7.0_13

Using Windows XP SP3, JDK 1.7.0_13

在Windows XP上,已将系统环境变量PATH/JAVA_HOME/CLASSPATH设置/添加为包括以下目录:

On Windows xp, the system environment variables PATH/JAVA_HOME/CLASSPATH have been set/added-to to include these directories:

C:\Documents and Settings\Administrator\My Documents\project\jar;
C:\Java\jdk1.7.0_13;
C:\Java\jdk1.7.0_13\bin;
C:\Java\jdk1.7.0_13\lib;

没有用户定义的变量可能会冲突. 来自终端的保证(我在更改环境变量后将其重新启动以便对其进行更新,以在Windows XP中设置环境变量,请参阅

There are no user-defined variables which may be conflicting. Assurance from the Terminal (which I restarted after changing the environment variables so that it would be updated, to set environment variables in windows xp, see Environment variables for java installation):

>echo %classpath%
  C:\Documents and Settings\Administrator\My Documents\project\jar;C:\Java\jdk1.7.0_13;C:\Java\jdk1.7.0_13\bin;C:\Java\jdk1.7.0_13\lib;

>echo %java_home%
  C:\Documents and Settings\Administrator\My Documents\project\jar;C:\Java\jdk1.7.0_13;C:\Java\jdk1.7.0_13\bin;C:\Java\jdk1.7.0_13\lib;

>echo %path%
  C:\WINDOWS\System32;... ...C:\Java\jdk1.7.0_13;C:\Java\jdk1.7.0_13\bin;C:\Java\jdk1.7.0_13\lib;C:\Documents and Settings\Administrator\My Documents\project\jar;

矫kill过正,是的...当它起作用时,我会适当修剪它们

Overkill, yes... when it works I'll trim them appropriately

相关文件:

C:\Documents and Settings\Administrator\My Documents\project\jar\jogl-all.jar
C:\Documents and Settings\Administrator\My Documents\project\Mush.java

C:\Documents and Settings\Administrator\My Documents\project\JarDir.java

jogl-all.jar 包含"javax.media.opengl.GL2", Mush.java 是一个程序,可通过导入java-OpenGL库显示简单的3D蘑菇;具体来说,它会导入"javax.media.opengl.GL2".

jogl-all.jar contains "javax.media.opengl.GL2", Mush.java is a program which displays simple 3D mushrooms through importing java-OpenGL libraries; specifically it imports "javax.media.opengl.GL2".

JarDir.java 是我在 http://www.java2s.com/Code/Java/File-Input-Output/Listfilesinajarfile.htm ,它显示了jar中的类,我用它来证明jar的内容.它还证明了可以在计算机上进行编译.

JarDir.java is a program I found at http://www.java2s.com/Code/Java/File-Input-Output/Listfilesinajarfile.htm which shows what classes are within a jar, I am using it to prove the jar contents. It also demonstrates that compilation is possible on the machine.

JarDir.java如下:

JarDir.java is as follows:

import java.io.*;
   import java.util.*;
   import java.util.jar.*;

   public class JarDir {
     public static void main (String args[]) 
         throws IOException {

       JarFile jarFile = new JarFile("C:\\Documents and Settings\\Administrator\\My Documents\\project\\jar\\jogl-all.jar");
       Enumeration enumy = jarFile.entries();
       while (enumy.hasMoreElements()) {
         process(enumy.nextElement());
       }
     }

     private static void process(Object obj) {
       JarEntry entry = (JarEntry)obj;
       String name = entry.getName();
       long size = entry.getSize();
       long compressedSize = entry.getCompressedSize();
       System.out.println(
           name + "\t" + size + "\t" + compressedSize);
     }
   }

并给出输出:

>javac JarDir.java

>java JarDir
... (lots of stuff)
javax/media/opengl/GL2.class   94123   28513
... (lots of stuff)

演示了成功的编译以及该类在jar文件中的存在.尝试运行 Mush.java 会产生很多错误,明显是第一个错误:

which demonstrates successful compilation and the presence of the class within the jar file. Attempting to run Mush.java gives many errors, significantly the first one:

>javac Mush.java
Mush.java:21: error: package javax.media.opengl does not exist:
import javax.media.opengl.GL2;
                         ^

要确定这是否是类路径问题,我运行了:

To determine if this was a classpath problem, I ran:

>javac Mush.java -cp ./jar/jogl-all.jar

基于缺少相关类(例如 MushScene.java )的错误而给出了错误,该类由 Mush.java 实例化并位于同一项目文件夹中. ..)但是找到javax.media.opengl没问题!因此,问题一定是环境变量问题!

Which gave errors based on the lack of presence of related classes (such as MushScene.java which is instantiated by Mush.java and lives in the same project folder ...) BUT no problems in finding javax.media.opengl ! So the problem must be an environment variable problem!

这意味着,尽管根据echo%classpath%正确设置了类路径,但是java并不同意.我做错了什么?

This then means, that although according to echo %classpath% the classpath is set correctly, java disagrees. What have I done wrong?

注意: Mush.java 程序已在Linux上正常运行.

Note: the Mush.java program has demonstratively worked on Linux.

注2:我有多个jar,如果最终在jar文件夹中使用了N个jar,我希望能够包含该文件夹,而不是专门为每个jar命名=)

Note2: I have multiple jars, if I end up using N jars within the jar folder, I'd like to be able to include the folder, rather than naming each jar specifically =)

推荐答案

jogl-all.jar不在CLASSPATH环境变量中.因此很明显,如果您没有在-cp选项中明确地包含它,则javac不能从此jar中找到任何类. jar本身必须在CLASSPATH中,而不是包含它的目录中.就像在-cp选项中一样.

jogl-all.jar is not in your CLASSPATH environment variable. So obviously, javac can't find any class from this jar if you don't include it explicitely in the -cp option. The jar itself must be in the CLASSPATH, and not the directory containing it. just like in the -cp option.

无论如何,我不会使用全局CLASSPATH环境变量,而只会使用-cp选项.当您编译或运行具有不同依赖关系的另一个Java项目时,它将使一切变得更容易.

I wouldn't use a global CLASSPATH environment variable anyway, but only the -cp option. It will make everything easier when you'll compile or run another Java project, with different dependencies.

这篇关于Java环境变量设置正确,但是Java找不到jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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