為什麼我的 IDE 能找到 JAR 而我的命令行卻沒有? (Why does my IDE find the JAR but my command line doesn't?)


問題描述

為什麼我的 IDE 能找到 JAR 而我的命令行卻沒有? (Why does my IDE find the JAR but my command line doesn't?)

I've tried searching around, but I couldn't find any answers that fit my case.

I can run the file CB.java just fine when I use an IDE.  This file depends on classes specified in cs2.jar.  Here are the contents of its directory.

02/12/2013  03:43 PM    <DIR>          .
02/12/2013  03:43 PM    <DIR>          ..
02/12/2013  03:45 PM             2,226 CB.class
02/12/2013  01:21 PM             2,164 CB.java
02/12/2013  03:43 PM            71,128 cs2.jar
               3 File(s)         75,518 bytes
               2 Dir(s)  408,977,362,944 bytes free

When I run it off my IDE, CB.java works just fine.  However, when I try java CB in the command line, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: sn/visual/JRect
angle
Caused by: java.lang.ClassNotFoundException: sn.visual.JRectangle
        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)
Could not find the main class: CB.  Program will exit.

Furthermore, I tried following suggestions to add something to the classpath using:

>java ‑cp C:\Users\...blah blah blah...\Software_Engineering cs2
Exception in thread "main" java.lang.NoClassDefFoundError: cs2
Caused by: java.lang.ClassNotFoundException: cs2
        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)
Could not find the main class: cs2.  Program will exit.

How come my IDE is smart but my command line isn't?

Thank you.

‑‑‑‑‑

參考解法

方法 1:

The class path is set to just consider .class files in the given directory. You need to add the jar file to the class path: java ‑cp C:\somewhere\cs2.jar

方法 2:

  

How come my IDE is smart but my command line isn't?

I suspect in your IDE, you've included the jar file in your build path, so it then includes it when both building and running. (You haven't told us which IDE it is, so it's hard to use the exact terminology it would use)

On the command line, you need to specify the jar file when both building and running too, so you'd use:

To build:

javac ‑cp cs2.jar CB

To run:

java ‑cp .;cs2.jar CB

(by user1971506C‑OttoJon Skeet)

參考文件

  1. Why does my IDE find the JAR but my command line doesn't? (CC BY‑SA 3.0/4.0)

#java #classpath #command-line #jar






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論