How to use acm.jar in eclipse? - java

I am learning Java by Stanford's Programming methodology videos on YouTube and I'm using the latest version of eclipse not the Stanford version. After I add acm.jar to the build path I still get a red underline under GLabel and error messages. This is what I've got:
import acm.program.*;
public class Artistry extends GraphicsProgram {
public void run() {
add(new GLabel("hello, world"), 100, 75);
}
}
Does anyone have any idea how to fix this? I really appreciate any help. Thanks :)

its because you also need to add: import acm.graphics.GLabel;
-or-
if you're going to use other G[something] you can add: import acm.graphics.*;

Related

How can I fix the project build path error? I can't import the java.lang.math for a Java 8/9 Calculator app

I am developing a simple calculator application in Java 8/9 in Eclipse. I am working on the power operation (as in "to the power of" used in math). I want to use the Math.power() instead of a for loop. However, I am having trouble importing the java math package into the program. The internet says to add import java.lang.math. When I try to code it in, I receive a notice of "Cannot Perform Operation. This compilation unit is not on the build path of the Java Project". What am I overlooking and/or doing wrong? Please provide suggestions or feedback.
Please note: Yes this is an academic assignment. To make this clear, I am not asking for the coding of the power operation. This issue is specifically about the importing the math package.
power operation (power.java)
package org.eclipse.example.calc.internal.operations;
import org.eclipse.example.calc.BinaryOperation;
// import java.lang.math; produces error
// Binary Power operation
public class Power extends AbstractOperation implements BinaryOperation {
// code removed. not relevant to SOF question.
}
Main (calculator.java)
package org.eclipse.example.calc.internal;
import org.eclipse.example.calc.BinaryOperation;
import org.eclipse.example.calc.Operation;
import org.eclipse.example.calc.Operations;
import org.eclipse.example.calc.UnaryOperation;
import org.eclipse.example.calc.internal.operations.Power;
import org.eclipse.example.calc.internal.operations.Equals;
import org.eclipse.example.calc.internal.operations.Minus;
import org.eclipse.example.calc.internal.operations.Plus;
import org.eclipse.example.calc.internal.operations.Divide;
import org.eclipse.example.calc.internal.operations.Square;
public class Calculator {
private TextProvider textProvider;
private String cmd;
private boolean clearText;
private float value;
public static String NAME = "Simple Calculator";
public Calculator(TextProvider textProvider) {
this.textProvider = textProvider;
setupDefaultOperations();
}
private void setupDefaultOperations() {
new Power();
new Equals();
new Minus();
new Plus();
new Divide();
new Square();
}
....
BTW, I use camel Case normally, but the academic project name everything including file names in standard writing format.
EDIT: After reading a response, I realized I forget to mention this. I can't get any further than typing import java., then the error pop-ups. Then I can't type the rest of the import statement
Image of package hierarchy
Your project is not configured correctly. You have no source dir at all. The src dir should be marked as source dir; right click it and tell eclipse about this, or, as it is a maven project, it's more likely a broken pom. Also, why are you using the org.eclipse package? If you work for SAP, it should be com.sap.

Eclipse cannot find JavaFX 11 Legend type

I'm trying to get some java 1.8 code working in Java 11. I've hit what seems to be a common wall, but none of the answers I've found are working.
System: Fedora 33 5.9.8-200.fc33.x86_64
Eclipse Version: 2020-09 (4.17.0) Build id: 20200910-1200
JDK: java-11-openjdk-11.0.9.11-0.fc33.x86_64
JavaFX: OpenFX javafx-sdk-11.0.2
Here's the bit of code that's blocking me.
class MyPie extends PieChart {
public Legend legend;
public MyPie() {
super();
legend = (Legend) getLegend();
}
}
The error is: "Legend cannot be resolved to a type" When I ask Eclipse to auto-generate an import statement, or use the suggestion, it put in this line:
import com.sun.javafx.charts.Legend;
This is a problem, because that doesn't seem to exist and doesn't follow the same pattern as the other JavaFX imports:
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
I have a second one "com.sun.javafx.css.StyleManager" which I'm guessing will have the same solution.
What am I missing here?
Thanks.

Why wont this run in BlueJ?

I am trying to do the free Stanford programming course online, but the programs wont run when executed. I believe I have successfully added the acm library to BlueJ, because when I compile the code, the imports are accepted, and it doesn't show any errors. But when I try and run the program, nothing happens, and all I see is the text, "Start: applet not initialized." Please, Please, Please help.
The code I am using in BlueJ is the following:
import acm.graphics.*;
import acm.program.*;
public class HelloProgram extends GraphicsProgram {
public void run() {
add(new GLabel("hello, world", 100, 75));
}
}
First of all you have to install the AMCInvoker extension, then you create an instance of your class HelloProgram and call the start method.

Error: cant find main class

I am a newbie java dev using netbeans IDE 7.1.1 and im watching this tutorial and right off the bat i get an error in my program even after 5 retypes to make sure its exactly the same as in the video so anyways this is the error
Error: Could not find or load main class javagame.JavaGame Java
Result: 1
and this is the code i have written
package JavaGame;
import javax.swing.JFrame;
public class JavaGame extends JFrame {
public JavaGame(){
setTitle("java game");
setSize(500, 500);
setResizable(false);
setVisible(true);
//setDefaultCloseOperation();
}
public static void main(String[] args){
}
}
You can find in this image how my project structure looks.
If that is an exactly copy-paste from your code, then your problem is with package name. Java is case sensitive and your package name is JavaGame so the qualified name of your class is JavaGame.JavaGame whilst the exception you get says that it can not find the class javagame.JavaGame.
Rename your package using Netbeans from JavaGame to javagame. That should fix the problem.
The problem is with your package name.. change the code at line1
package JavaGame;
package javagame;
NetBeans usually likes to use lower text for packages.
Hope this solves the problem. As the code runs on my machine.
Phil
Go to environmental variables and add path of JDK.
I got this error in past, and solved by setting classpath.

Javascript to Java communication using LiveConnect not working

I've been working on a project that requires communication both directions between Java and JavaScript. I have successfully managed to get it working under all browsers in OS X, but I'm now faced with the challenge of getting it to run on Windows under any browser. At the moment it simply doesn't work.
I'm just wondering if there is something special I need to do in order for JavaScript to communicate with Java?
My applet code looks like this:
<applet id='theApplet'
code="com/company/MyApplet.class"
archive="SMyApplet.jar"
height="50" width="900"
mayscript="true" scriptable="yes">
Your browser is ignoring the applet tag.
</applet>
Once the applet has loaded, I then try to call functions on it like this:
alert("Call some java:" + theApplet.testFunc());
And in the firebug console I get the following error:
theApplet.testFunc is not a function
I can confirm that this doesn't work in IE either.
When the page loads, I have the java console open and I can see that the applet is successfully loading and ready to accept calls.
Any help would be greatly appreciated!
Cheers
Update: Here is the stripped down java code exposing the public api that I'm trying to call.
package com.company;
import com.google.gson.Gson;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
import netscape.javascript.*;
public class MyApplet extends Applet implements Runnable
{
public void init()
{
JSON = new Gson();
isReadyVar = 0;
workThread = null;
}
public void start()
{
}
public void run()
{
System.out.println("Done");
}
public void stop()
{
}
public void destroy()
{
}
/* Public API */
public int testFunc()
{
return 200;
}
}
Update [SOLVED]:
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
alert("Call some java:" + document.getElementbyId("theApplet").testFunc());
Make sure the testFunc() method is declared as public access.
If that does not work, post the applet code as an SSCCE.
BTW
Incorrect
code="com/company/MyApplet.class"
Correct
code="com.company.MyApplet"
BTW 2
Incorrect
..scriptable="yes">
Correct
..scriptable="true">
Since the applet element is deprecated, I use following code, which works at least in Firefox:
<object id="MyApplet" classid="java:com.example.myapplet"
codetype="application/java" codebase="bin/" height="10" width="10"
</object>

Categories