Why wont this run in BlueJ? - java

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.

Related

Why the exported to desktop (.exe) processing app with java classes can not run properly? My another project was exported without problems

I try to export my processing app as the "ready to use" application with exe launch file for desktop.
My code is pretty simple. Proccessing code:
//import io.itch.mgdsstudio.airfight.connecteddevices.*;
private io.itch.mgdsstudio.airfight.connecteddevices.Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
//I think the next code line can not be launched after export. Background stays black
controller = new io.itch.mgdsstudio.airfight.connecteddevices.Controller(this);
}
void draw(){
controller.render();
}
and the java-class:
package io.itch.mgdsstudio.airfight.connecteddevices;
import processing.core.PApplet;
public class Controller {
private PApplet engine;
public Controller(PApplet engine) {
this.engine = engine;
}
public void render(){
engine.background(255,0,0);
}
}
The application runs perfect from processing ide - the screen is red. But after export it can not run properly. The screen is black. I tested processing 3 and 4. In january I exported an another application succesfully. But now I can not launch exported file. I think the trouble is in versions of the java source files.
I tried to change code so:
import io.itch.mgdsstudio.airfight.connecteddevices.Controller;
private Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
controller = new Controller(this);
}
void draw(){
controller.render();
}
I receive the message in the console:
No library found for io.itch.mgdsstudio.airfight.connecteddevices
but it runs in the ide. But after export it can not run properly again. Maybe i need another package names?
There seems to be an issue/bug using package inside the Processing IDE.
As you hinted in your comments, if you could compile a .jar in your Java IDE of choice and use that in Processing that should work.
From a pragmatic point of view, if you don't plan on fixing the Processing IDE to handle package as expected, you can simply not use it your code:
import processing.core.PApplet;
public class Controller {
private PApplet engine;
public Controller(PApplet engine) {
this.engine = engine;
}
public void render(){
engine.background(255,0,0);
}
}
Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
controller = new Controller(this);
}
void draw(){
controller.render();
}
This wouldn't be convenient on a larger project where you may want to write Java code in your preferred IDE but still be able to colaborate with others that use the Processing IDE alone.
Since running in the Processing IDE works (e.g. dev/testing), but can't export a .exe for easy deployment, a workaround could be using the processing-java command. (If you add your Processing installation folder to the Windows %PATH% environment variable you should be able to type processing-java in a new Command Prompt window and see the help/usage guide).
Running processing-java --sketch=path\to\YourSketchFolder --run should run the sketch the same way the Processing IDE does (without actually having the IDE open). While this implies installing Processing and adding it to %PATH% on any new machine on one hand, on the other it saves you the time of having rebuild/repackage new .exe files for every single change.

How to correctly create and import packages in Java

I am using Netbeans 8.1 and Java 8.
I have a Java program named "MyFrame.java" and I want to create a package with its classes and methods - I call this package "myframe" and it is located at "\Lab\MyFrame\src\myframe". See picture:
(Ignore the red lines - this is a dummy version).
The class file is created after compiling, using the command "javac MyFrame.java", in the same directory \myframe. Now I want to import the package "myframe" in a new Java file "MoreButtons.java". So it would look like this and for convenience I save it in \src:
Compiling and executing MoreButtons.java works fine. The package has been imported. But now MyFrame.java is a bit trickier to execute: the naïve approach yields:
Translation: Error: Could not find or load main class
This seems to be quite a common problem and one of the solutions is simply to add the directory (\myframe) to the PATH environment variable. However, doing this still produced the error.
1) What am I doing wrong and how can I fix this?
2) What is the correct way to create and import custom-made packages in Java?
Make sure that terminal is at path Lab\MyFrame\src:
javac myframe\MyFrame.java MoreButtons.java
java -cp .; myframe.MyFrame
P.S. (/,:=linux/mac) or (\,;=windows)
MyFrame.java
package myframe;
public class MyFrame extends javax.swing.JFrame{
public MyFrame(String title){
super(title);
setSize(200,100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
MoreButtons.java
public class MoreButtons {
public static void main(String[]args){
new myframe.MyFrame("More Buttons");
}
}

Why do I get "LoadLibrary failed with error 1114: a dynamic link library (DLL) initialization routine failed"?

When I run my java application program an error window appears saying that
"LoadLibrary failed with error 1114: a dynamic link library (DLL) >initialization routine failed".
I have tested my code on a different machine and it worked perfectly.The program shows a PApplet window with a map inside.However, Running the code on my laptop, the PApplet appears and all of the sudden the DLL error stops the rest from being shown.
What the problem could be and how can I fix it?
Here is the code I am trying to run. It is worth to mention that it runs successfully if I remove what's inside the setup() method.
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.utils.MapUtils;
import processing.core.PApplet;
public class LifeExpectancy2 extends PApplet {
UnfoldingMap map;
public void setup()
{
size(800,600,OPENGL);
map = new UnfoldingMap (this, 50, 50, 700, 500, new Google.GoogleMapProvider());
MapUtils.createDefaultEventDispatcher (this, map);
}
public void draw()
{
}
}
I had the same issue after I installed my Netbeans to build some projects in PHP and it was fixed changing some graphics options in the control painel of my Windows 10.
Take a look on this video and see if it fixs your issue as well:
Windows 10 - Java Loadlibrary Error 1114
I hope it can be helpful!
Which Unfolding version did you download? You seem to use some Java IDE (and not Processing's one) so you need the Unfolding-for-Eclipse distribution which includes all needed native libraries (i.e. also the DLL in question).
For the records, the DLL is the native library for Windows OS to bind Java to the OpenGL API (JOGL).

How to use acm.jar in eclipse?

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.*;

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.

Categories