我有一个程序由于找不到某些库而无法编译。我尝试使用-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
答案 0 :(得分:3)
编译:javac -cp B:\ Programming \ Java \ Compiler \ lib * ThermDemo.java
运行:java ThermDemo
将其更改为:
java -cp .;B:\Programming\Java\Compiler\lib* ThermDemo
运行时类路径需要包含(至少)所有编译时依赖项。 “。;”需要告诉java
查看当前目录...找到刚刚编译的“.class”文件。
实际上,此解决方案仅适用于您当前的问题。类路径的工作方式有很多复杂性,建议您花点时间阅读它。这是一个很好的起点:
(我假设ThermoDemo.java
有main
方法并带有相应的签名。)
答案 1 :(得分:0)
ThermDemo.class
是您必须运行的类。 ThermDemo$ContentPanel.class
是内部类。编译具有内部类的类时,将生成两个类文件。