Including all the jars in a directory within the Java classpath

前端 未结 24 3539
鱼传尺愫
鱼传尺愫 2020-11-21 04:25

Is there a way to include all the jar files within a directory in the classpath?

I\'m trying java -classpath lib/*.jar:. my.package.Program and it is no

24条回答
  •  余生分开走
    2020-11-21 05:02

    All the above solutions work great if you develop and run the Java application outside any IDE like Eclipse or Netbeans.

    If you are on Windows 7 and used Eclipse IDE for Development in Java, you might run into issues if using Command Prompt to run the class files built inside Eclipse.

    E.g. Your source code in Eclipse is having the following package hierarchy: edu.sjsu.myapp.Main.java

    You have json.jar as an external dependency for the Main.java

    When you try running Main.java from within Eclipse, it will run without any issues.

    But when you try running this using Command Prompt after compiling Main.java in Eclipse, it will shoot some weird errors saying "ClassNotDef Error blah blah".

    I assume you are in the working directory of your source code !!

    Use the following syntax to run it from command prompt:

    1. javac -cp ".;json.jar" Main.java

    2. java -cp ".;json.jar" edu.sjsu.myapp.Main

      [Don't miss the . above]

    This is because you have placed the Main.java inside the package edu.sjsu.myapp and java.exe will look for the exact pattern.

    Hope it helps !!

提交回复
热议问题