I have a program that will not compile due to certain libraries not being found. I have tried using the -cp %pathTo\lib%/* command to include all .jar files. That gets it to compile but then when I try to run the program it tells me it cannot find or load the main class.
So I want to manually include each .jar file, my only problem is I don't know which exact ones to include.
/* ---------------------
* 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);
}
}
Commands:
to compile:
javac -cp B:\Programming\Java\Compiler\lib* ThermDemo.java
to run: java ThermDemo
After compiling there are two new files in my current directory.
ThermDemo$ContentPanel.class, and ThermDemo.class
to compile: javac -cp B:\Programming\Java\Compiler\lib* ThermDemo.java
to run: java ThermDemo
Change that to:
java -cp .;B:\Programming\Java\Compiler\lib* ThermDemo
The runtime classpath needs to include (at least) all of the compile-time dependencies. The ".;" is needed to tell java to look in the current directory as well ... to find the ".class" files you just compiled.
Actually, this solution only applies to your current problem. There is a lot of complexity in the way that classpaths work, and you would be advised to spend time reading up on it. Here's a good place to start:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
(I'm assuming that ThermoDemo.java has a main method with the appropriate signature.)
ThermDemo.class is the class that you have to run. ThermDemo$ContentPanel.class is the inner class. When you compile a class having inner classes, both class files will be generated.
Related
I have a Java Application, I am wanting to format a button as either Active or Inactive (Also possibly a hover method).
The code as I would like to implement it:
//Home Tab - Active by default
home = new TabButton();
home.setSize(new Dimension(tabWidth, tabHeight));
home.setFont(getLauncherFont(34));
home.setForeground(Color.white);
home.setText("HOME");
home.setBounds(160, 0, tabWidth, tabHeight);
home.setActive(); --> This Method is what I would like to create
I already have a class to create a JButton for the tab:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
}
I have researched abstract classes. But my TabButton class already extends JButton.
I would like a method like this:
public void setActive(){
this.setBackground(Color.red);
//Any other changes a want to make regularly
}
That can simply be implemented like this home.setActive();
My Question I suppose is: Is it easy enough to implement what I am looking for, or will I have to got the long way and set all attributes manually every time?
What you've described in the post can be done like this:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {// initialize
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
// add your own methods or override JButton methods
public void setActive(){
//Add code
//example: setEnabled(true);
}
}
Okay, so I've done Swing applications before, and I know if you want to display a different name for the application menu (the one on Macs that usually have a "Preferences" and "Quit" option), you have to use: System.setProperty("com.apple.mrj.application.apple.menu.about.name", "App name"); and it must be executed before the JFrame is created. I've done this, but it continues to show my Main class' name as the menu name, as if I didn't write that line of code at all. I googled for this issue, but couldn't find anything useful, and then I just searched on here, but everyone who had a similar issue was running Java 1.5, 1.6, or 1.7. So I thought maybe it had something to do with my current Java version, 1.8.
This, this, and this did not work. This, this, and this either sent me to outdated info, or the links didn't work anymore. Also, I'm running Mac 10.8.
Any suggestions/answers would be greatly appreciated.
An update:
here is the code I originally had:
package bouncing_off_axes;
/**
* This is the driver class of this program.
*
* #author Mason
*
*/
public class Main {
/**
* The driving method.
*
* #param args
*/
public static void main(String[] args) {
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Physics Engine Practice - Bouncing Balls");
SimulationController view = new SimulationController("Test");
}
}
And here is a solution that trashgod provided to someone else:
package bouncing_off_axes;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
/** #see https://stackoverflow.com/questions/8955638 */
public class NewMain {
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty(
"com.apple.mrj.application.apple.menu.about.name", "Name");
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Gabby");
final JPanel dm = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
dm.setBorder(BorderFactory.createLineBorder(Color.blue, 10));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dm);
frame.pack();
frame.setLocationByPlatform(true);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
});
}
}
And apparently I need 10 reputation to post images, so I can't show you the result, but it didn't work out.
Using Java 8 on Mac OS X 10.9, setting the System property
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Name");
appears to be ineffectual in changing the name displayed in the application menu. Alternatives that still work include these:
By default, the application menu will display the simple name of the class specified in the java command line or the Main-Class attribute of the JAR's manifest.
java -cp build/classes mypackage.Name
java -jar dist/MyJar.jar
Use the -Xdock:name="Name" command line parameter.
java -Xdock:name="Name" -cp build/classes mypackage.Main
java -Xdock:name="Name" -jar MyJar.jar
Add the application JAR to a Mac application bundle, as shown here, an add the attribute to the Info.plist.
<key>JVMOptions</key>
<string>-Xdock:name=Name</string>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to write a program that copies one file and copies it's contents to another. I have to have user chose the files. i am stuck can some one please help me.
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import javax.swing.JFileChooser;
public class FileCopy {
public static void main(String[]Args) throws IOException {
JFileChooser chooser = new JFileChooser("/Users/josealvarado/Desktop/");
Path FROM = Paths.get(chooser);
Path TO = Paths.get(chooser);
//overwrite existing file, if exists
CopyOption[] options = new CopyOption[]{
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES
};
Files.copy(FROM, TO, options);
}
}
I only can guess, but i think you'll need a JFrame which will contain your JFileChooser, i made a small example without any functionality, only to show, how you could maybe reach your goal.
Please realize for your next question(s) here on SO, post what you tried and POST the errors / exception you get, otherwise it is hard to help or solve your problem!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.professional_webworkx.tutorial.jtable.view;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
/**
*
* #author ottp
*/
public class MainFrame extends JFrame {
private JFileChooser chooser;
public MainFrame() {
initGUI();
}
private void initGUI() {
chooser = new JFileChooser();
chooser.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(chooser.getSelectedFile().getName());
}
});
this.setTitle("FileChoosing and copy one file to another");
this.setSize(1024, 768);
this.getContentPane().add(chooser, BorderLayout.NORTH);
this.setVisible(true);
}
}
The Class to start your App
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.professional_webworkx.tutorial.jtable;
import de.professional_webworkx.tutorial.jtable.view.MainFrame;
import javax.swing.JFileChooser;
/**
*
* #author ottp
*/
public class JTableDemo {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new MainFrame();
}
}
Hope this helps,
Patrick
Path FROM = Paths.get(chooser);
Path TO = Paths.get(chooser)
You can't pass a JFileChooser to Paths.get(). Here are the overloaded static methods
Paths.get(String first, String... more) - Converts a path string, or a sequence of strings that when joined form a path string, to a Path.
Paths.get(URI uri) - Converts the given URI to a Path object.
You're probably looking to pass a String. In order to to that, you need to get the String file path from the JFileChooser. To do that, you first need to chooser.showOpenDialog() which returns an int if the OK button is pressed after selecting a file (APPROVE_OPTION), So you want to do something like this
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(null);
String path;
if (result == JFileChooser.APPROVE_OPTION) {
path = (chooser.getSelectedFile()).getAbsolutePath();
}
Then you can pass the path to Paths.get(path)
You should really have a look at How to Use File Choosers
Is all in the title,
I do not understand the problem this time is a bit different, I used the same Object(List) for two different programs and it does not work in the second time, see :
private void jMenuItem23ActionPerformed(java.awt.event.ActionEvent evt) {
init_creer_client();
List items = new ArrayList();
items.add("mawren");
items.add("blabla");
items.add("Bonjour");
CL.show(cartes,"creer_client");
}
screenshot about the error :
by cons here its work smoothly :
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
public class Test_swingx extends JFrame {
public Test_swingx(String title) throws HeadlessException {
this.setTitle(title);
JPanel pan=new JPanel();
JTextField jtf=new JTextField();
jtf.setColumns(20);
List items = new ArrayList();
items.add("hello");
items.add("marwen");
items.add("allooo");
AutoCompleteDecorator.decorate(jtf, items,false);
pan.add(jtf);
this.setContentPane(pan);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setBounds(280, 150, 500, 200);
}
public static void main(String[] args) {
Test_swingx tsx=new Test_swingx("helloo swingx");
}
}
can anyone explain to me ?
You have a java.awt.List import should be java.util.List
It's because the List on the left-hand side is a java.awt.List instead of a java.util.List.
Try changing the line to:
java.util.List items = new ArrayList();
This is probably happening because you're importing java.awt.* and java.util.List. If you can change how you import these classes, you can avoid namespacing the type inline.
Nope, compiles fine:
package cruft;
import java.util.ArrayList;
import java.util.List;
/**
* ListExample description here
* #author Michael
* #link
* #since 2/11/12 7:27 PM
*/
public class ListExample {
public static void main(String[] args) {
List items = new ArrayList();
for (String arg : args) {
items.add(arg);
}
System.out.println(items);
}
}
Runs fine:
"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 111.255\bin" -Dfile.encoding=UTF-8 -classpath . com.intellij.rt.execution.application.AppMain cruft.ListExample foo bar baz bat
[foo, bar, baz, bat]
Process finished with exit code 0
Sanity check: Have you imported both import java.util.List and import java.util.ArrayList?
Check your imports, because java.awt.List is not the same as java.util.List.
I think the confusion comes from having two List types in different packages, as the error message says. You don't give all the code that generates the error, but I think a reasonable start to a fix would be to change the highlighted line to:
java.util.List items = new ArrayList();
and make sure you have imported java.util.*
Can someone please give me some extra basic example of how jzy3d should be used?
(The source site's examples don't seam to work for me)
I tried the following code:
import org.jzy3d.chart.Chart;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.builder.Mapper;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.plot3d.primitives.Shape;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
Chart chart = getChart();
frame.add((javax.swing.JComponent) chart.getCanvas());
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
frame.setTitle("test");
frame.setVisible(true);
}
public static Chart getChart() {
// Define a function to plot
Mapper mapper = new Mapper() {
public double f(double x, double y) {
return 10 * Math.sin(x / 10) * Math.cos(y / 20) * x;
}
};
// Define range and precision for the function to plot
Range range = new Range(-150, 150);
int steps = 50;
// Create the object to represent the function over the given range.
org.jzy3d.plot3d.primitives.Shape surface = (Shape) Builder.buildOrthonormal(new OrthonormalGrid(range, steps, range, steps), mapper);
surface.setColorMapper(new ColorMapper(new ColorMapRainbow(), surface.getBounds().getZmin(), surface.getBounds().getZmax(), new Color(1, 1, 1, .5f)));
surface.setWireframeDisplayed(true);
surface.setWireframeColor(Color.BLACK);
//surface.setFace(new ColorbarFace(surface));
surface.setFaceDisplayed(true);
//surface.setFace2dDisplayed(true); // opens a colorbar on the right part of the display
// Create a chart
Chart chart = new Chart();
chart.getScene().getGraph().add(surface);
return chart;
}
}
But when I try to run it, I get that exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl in java.library.path
Can anyone help?
You should add jogl.jar to classpath and jogl.dll to PATH.
For more info look here and here.
You can read jogl Installation Instructions here.
You should run your program or demo where the JOGL native libraries stand, i.e. ./bin/{platform}. If you are working with Eclipse, right click on the project, choose Propeties, Java Build Path then the Libraries tab. Under the item "jogl.jar - ..." select "Native library location: (None)" and click the Edit button. Press the Workspace... button and select the ./bin/{platform} folder.