简体   繁体   English

Java环境变量设置正确,但是Java找不到jar

[英]Java environment variables set correctly, but java is not finding the jars

Summary 摘要

Java environment variables correctly set, as seen from echo-ing them. 从回显它们可以看出,Java环境变量设置正确。 However, jars are only seen when explicitly included with javac command. 但是,只有在javac命令显式包含jar时,才能看到jar。

Details 细节

This is not specifically related to graphics and OpenGl, certain the problem is related to classpaths, ie the importing of third-party jar files. 这与图形和OpenGl没有特别的关系,某些问题与类路径有关,即第三方jar文件的导入。

Using Windows XP SP3, JDK 1.7.0_13 使用Windows XP SP3,JDK 1.7.0_13

On Windows xp, the system environment variables PATH/JAVA_HOME/CLASSPATH have been set/added-to to include these directories: 在Windows xp上,已将系统环境变量PATH / JAVA_HOME / 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;

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 ): 从终端获得保证(我在更改环境变量以使其可以更新后重新启动,以在Windows XP中设置环境变量,请参阅Java安装的环境变量 ):

>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;

Overkill, yes... when it works I'll trim them appropriately 过度杀伤,是的...当它起作用时,我会适当地修剪它们

Relevant files: 相关文件:

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 contains "javax.media.opengl.GL2", Mush.java is a program which displays simple 3D mushrooms through importing java-OpenGL libraries; jogl-all.jar包含“ javax.media.opengl.GL2”, Mush.java是通过导入Java-OpenGL库显示简单3D蘑菇的程序; specifically it imports "javax.media.opengl.GL2". 具体来说,它会导入“ javax.media.opengl.GL2”。

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. JarDir.java是我在http://www.java2s.com/Code/Java/File-Input-Output/Listfilesinajarfile.htm中找到的程序,它显示了jar中的类,我正在使用它来证明jar的内容。 It also demonstrates that compilation is possible on the machine. 它还表明可以在计算机上进行编译。

JarDir.java is as follows: JarDir.java如下:

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);
     }
   }

and gives the output: 并给出输出:

>javac JarDir.java

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

which demonstrates successful compilation and the presence of the class within the jar file. 演示了成功的编译以及该类在jar文件中的存在。 Attempting to run Mush.java gives many errors, significantly the first one: 尝试运行Mush.java会产生很多错误,其中第一个明显是:

>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

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 ! 这基于缺乏相关类的存在(如MushScene.java这是由Mush.java实例化,并住在同一个项目文件夹...),但在寻找javax.media.opengl没有问题了错误! So the problem must be an environment variable problem! 因此,问题一定是环境变量问题!

This then means, that although according to echo %classpath% the classpath is set correctly, java disagrees. 这意味着,尽管根据echo%classpath%正确设置了类路径,但java对此表示不同。 What have I done wrong? 我做错了什么?

Note: the Mush.java program has demonstratively worked on Linux. 注意: Mush.java程序已在Linux上正常工作。

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

jogl-all.jar is not in your CLASSPATH environment variable. jogl-all.jar不在CLASSPATH环境变量中。 So obviously, javac can't find any class from this jar if you don't include it explicitely in the -cp option. 因此,很显然,如果您没有在-cp选项中明确地包含它, javac将无法从此jar中找到任何类。 The jar itself must be in the CLASSPATH, and not the directory containing it. jar本身必须在CLASSPATH中,而不是包含它的目录中。 just like in the -cp option. 就像在-cp选项中一样。

I wouldn't use a global CLASSPATH environment variable anyway, but only the -cp option. 无论如何,我不会使用全局CLASSPATH环境变量,而只会使用-cp选项。 It will make everything easier when you'll compile or run another Java project, with different dependencies. 当您编译或运行具有不同依赖关系的另一个Java项目时,它将使一切变得容易。

The solution for doing it with environment variables. 使用环境变量执行此操作的解决方案。 (Warning - may not be best practice, see accepted answer) (警告-可能不是最佳做法,请参见可接受的答案)

Classpath environment variable: 类路径环境变量:

C:\Documents and Settings\Administrator\My Documents\project\jar\*;

Path environment variable: 路径环境变量:

...;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;C:\Documents and Settings\Administrator\My Documents\project\lib;

Note that adding the jar folder is wrong - you need the backslash and the star: " jar\\* " in your classpath variable. 请注意,添加jar文件夹是错误的-您需要在类路径变量中使用反斜杠和星号“ jar \\ *”。 This adds all the jars in that folder. 这会将所有jar添加到该文件夹​​中。

As for the path variable, as well as the bin folder of your sdk, you also need the lib folder of your sdk, AND any lib folders which your jars expect to use. 至于path变量以及sdk的bin文件夹,您还需要sdk的lib文件夹,以及jars希望使用的所有lib文件夹。

Then you can javac Mush.java and java Mush and watch as everything works perfectly =) 然后,您可以javac Mush.javajava Mush并观察一切正常=)

Be careful not to use semicolons in your JAVA_HOME variable, since it would ruin all convenient paths like: %JAVA_HOME%\\bin . 注意不要JAVA_HOME变量中使用分号,因为它会破坏所有方便的路径,例如: %JAVA_HOME%\\bin As you have written it above, it would result in: C:\\Java\\jdk1.7.0_13;\\bin which is obviously wrong. 如您在上面所写,它将导致: C:\\Java\\jdk1.7.0_13;\\bin显然是错误的。

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

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