java - 确定使用哪些 .jar 文件

标签 java jar

由于找不到某些库,我的程序无法编译。我尝试使用 -cp %pathTo\lib%/* 命令来包含所有 .jar 文件。这使它可以编译,但是当我尝试运行该程序时,它告诉我它无法找到或加载主类。

所以我想手动包含每个 .jar 文件,我唯一的问题是我不知道要包含哪些确切的文件。

/* ---------------------
 * ThermometerDemo1.java
 * ---------------------
 * (C) Copyright 2002-2007, by Object Refinery Limited.
 *
 */


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

/**
 * A simple demonstration application showing how to create a thermometer.
 */
public class ThermDemo extends ApplicationFrame {

static class ContentPanel extends JPanel implements ChangeListener {

    JSlider slider;

    DefaultValueDataset dataset;

    /**
     * Default constructor.
     */
    public ContentPanel() {
        super(new BorderLayout());
        this.slider = new JSlider(0, 100, 50);
        this.slider.setPaintLabels(true);
        this.slider.setPaintTicks(true);
        this.slider.setMajorTickSpacing(25);
        this.slider.addChangeListener(this);
        add(this.slider, BorderLayout.SOUTH);
        this.dataset = new DefaultValueDataset(this.slider.getValue());
        add(new ChartPanel(createChart(this.dataset)));
    }

    private static JFreeChart createChart(ValueDataset dataset) {
        ThermometerPlot plot = new ThermometerPlot(dataset);
        JFreeChart chart = new JFreeChart(
            "Thermometer Demo 1",  // chart title
            JFreeChart.DEFAULT_TITLE_FONT,
            plot,                  // plot
            true                  // no legend
        );               

        plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setPadding(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
        plot.setThermometerStroke(new BasicStroke(2.0f));
        plot.setThermometerPaint(Color.lightGray);
        plot.setUnits(ThermometerPlot.UNITS_FAHRENHEIT);
        plot.setGap(3);
        return chart;       
    }

    public void stateChanged(ChangeEvent e) {
        this.dataset.setValue(new Integer(this.slider.getValue()));
    }

}

/**
 * Creates a new demo.
 *
 * @param title  the frame title.
 */
public ThermDemo(String title) {
    super(title);
    JPanel chartPanel = createDemoPanel();
    setContentPane(chartPanel);
}

/**
 * Creates a panel for the demo (used by SuperDemo.java).
 * 
 * @return A panel.
 */
public static JPanel createDemoPanel() {
    return new ContentPanel();
}

/**
 * Starting point for the demonstration application.
 *
 * @param args  ignored.
 */
public static void main(String[] args) {
    ThermDemo demo = new ThermDemo("Thermometer Demo 1");
    demo.pack();
    demo.setVisible(true);
    }

}

命令:

编译: javac -cp B:\Programming\Java\Compiler\lib* ThermDemo.java

运行:java ThermDemo

编译后,我的当前目录中有两个新文件。 ThermDemo$ContentPanel.class 和 ThermDemo.class

最佳答案

to compile: javac -cp B:\Programming\Java\Compiler\lib* ThermDemo.java

to run: java ThermDemo

将其更改为:

java -cp .;B:\Programming\Java\Compiler\lib* ThermDemo

运行时类路径需要包含(至少)所有编译时依赖项。这 ”。;”还需要告诉 java 在当前目录中查找...以找到您刚刚编译的“.class”文件。

实际上,这个解决方案仅适用于您当前的问题。类路径的工作方式非常复杂,建议您花时间阅读它。这是一个很好的起点:

<小时/>

(我假设 ThermoDemo.java 有一个带有适当签名的 main 方法。)

关于java - 确定使用哪些 .jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937815/

相关文章:

java - 如果我在循环内以编程方式运行垃圾收集器。它会影响我的 Android 应用程序吗?

Java:编辑并重新编译.jar?

java - java -jar 的类路径问题

android - 将.so添加到Xamarin android绑定(bind)库

java - 如何在JSP中调用JS函数?

java - 反射 java 接口(interface)时收到异常

java - 在java字符串中添加引号

Java 匹配器无法找到最后一组

debugging - 如何使用 IntelliJ 从 jars(非 JDK)进入代码?

java - 开发 Web 应用程序时,我应该将外部 JAR 文件放在哪里?