I/O using windowbuilder in eclipse for a java application - java

I've never used java windowbuilder before, however I'm trying to test it with my program which performs operations on sets. It's a Gradle project. I wrote all classes in the default package (I knew that it's discouraged just when I was finished). The program reads a line of operations on sets, parses it and prints the result and keeps doing that while there is a new line input from the user.
I'm trying to make a simple GUI for this program using windowbuilder but I can't figure out how to run the main class in the windowbuilder class and make it take input from a jtextfield and prints output.
My main looks like this:
public static void main(String[] argv) {
new Main().start();
}
private void start() {
hmap = new HashMap<IdentifierInterface, SetInterface<BigInteger>>();
Scanner in = new Scanner(System.in);
// While there is input, read line and parse it.
while (in.hasNextLine()) {
try {
String statement = in.nextLine();
if (statement.trim().isEmpty()) {
System.out.println("error, no statement");
} else {
Scanner statementScanner = new Scanner(statement);
readStatement(statementScanner);
}
} catch (APException e) {
System.out.printf("%s\n", e.getMessage());
}
}
}
I made a new windowbuilder class, with the buttons and text fields, but I got stuck on how to run my main inside the windowbuilder. Your help is appreciated. Thanks in advance.

Eclipse RCP applications are OSGi plug-ins; they do not have a main() method.
Instead your Main class should look like this:
package com.myplugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Main implements BundleActivator {
#Override
public void start(BundleContext bundleContext) throws Exception {
// Copy your start logic here
}
}
Then edit META-INF/MANIFEST.MF file and set for example
Bundle-Activator: com.myplugin.Main
This makes your Main class the activator of the plug-in: start() will be invoked at load.

Related

voce - Simple Java code not working

I recently installed Voce and am using it in my Java application. In Eclipse, I added the JAR files in voce-0.9.1/voce-0.9.1/lib to the libraries in my Java Build Path. From the documentation, I figured that this code should work:
import voce.SpeechSynthesizer;
public class Test
{
public static void main(String[] args)
{
// System.out.println("Speaking");
SpeechSynthesizer speaker = new SpeechSynthesizer("speaker");
speaker.synthesize("hello");
// System.out.println("Spoken");
speaker.destroy();
}
}
When I uncomment the output statements, the program outputs "Speaking", then almost immediately outputs "Spoken" without having made a sound. My volume is turned up. Does synthesize() speak the string or have I forgotten some initialization? And can you give me some code that should work?
Answer
Here is the code that worked (I used the static methods in SpeechInterface instead of a SpeechSynthesizer object):
public class Test
{
public static void main(String[] args)
{
voce.SpeechInterface.init("../../../lib", true, false, "", "");
System.out.println("Speaking");
voce.SpeechInterface.synthesize("hello");
System.out.println("Spoken");
try { Thread.sleep(1000); } catch(Exception ex) {}
voce.SpeechInterface.destroy();
}
}

ClassNotFoundException with JGroups

I have an issue with JGroups where after building my project, running it produces this error:
Caused by: java.lang.ClassNotFoundException: org.jgroups.ReceiverAdapter
My class looks something like this -
import org.jgroups.ReceiverAdapter;
import org.jgroups.Channel;
import org.jgroups.JChannel;
public class MyClass extends ReceiverAdapter implements MyInterface {
Channel channel;
String state = "state";
public MyClass() {
super();
start();
}
public void start() {
try {
channel = new JChannel();
channel.setReceiver(this);
channel.connect("ServerCluster");
channel.getState(null, 0);
System.out.println("Connected to cluster");
} catch (Exception e) {
System.out.println("Failed to connect to cluster");
}
}
public void getState(OutputStream output) throws Exception {
System.out.println("get response");
}
public void setState(InputStream input) throws Exception {
System.out.println("set test");
}
}
Running the project from IntelliJ produces no errors, but does not produce the desired prints from getState() and setState() either. I tried creating a brand new project in the Eclipse IDE, but the same is happening there too. Connecting has been working fine, states is a new addition to my project.
Running java MyClass from the command line fires the error seen at the start of this question. The JGroups jar seems to be added to the classpath properly as org.jgroups.Channel and org.jgroups.Channel (among others) are being found.
There is a SimpleChat program provided by the JGroup devs, but when I created a new project for this I encountered the same problem.
Edit
So it turns out I have to explicitly set the classpath when running from the CLI. But still, when running the code it seems like the getState() and setState() methods are never called as there are no print statements. SimpleChat doesn't print received state... like it is meant to.
Does anyone have a solution?
Best.
So, I on the JChannel I was using RpcDispatcher and it seems I can't use the dispatcher and the getState() and setState() methods on the same channel. Simple solution: create a second channel. Seems my knowledge on the fundamentals of JGroups is lacking!

JACOB event registration

I'm trying to use JACOB to obtain a callback whenever a slide show starts or ends using the following:
public class Test {
public static void main(String[] args) {
ActiveXComponent oApp = new ActiveXComponent("PowerPoint.Application");
Dispatch presentations = oApp.getProperty("Presentations").toDispatch();
Dispatch presentation = Dispatch.call(presentations, "Open", "C:\\Users\\Bob\\Documents\\test.ppt").toDispatch();
new DispatchEvents(oApp, new Handler());
}
}
public class Handler {
public void SlideShowBegin(Variant[] args) {
System.out.println("here");
}
}
However, I'm coming a bit unstuck, the result of the above is:
GetEventIID: couldn't get IProvideClassInfo
Exception in thread "main" com.jacob.com.ComFailException: Can't find event iid
at com.jacob.com.DispatchEvents.init3(Native Method)
at com.jacob.com.DispatchEvents.<init>(DispatchEvents.java:138)
at com.jacob.com.DispatchEvents.<init>(DispatchEvents.java:99)
at com.jacob.com.DispatchEvents.<init>(DispatchEvents.java:72)
at tester.Test.main(Test.java:28)
Does anyone have any ideas? Searching has come up pretty short. I've tried using the 4 argument constructor of DispatchEvents, supplying "Powerpoint.Application" and the full path to the powerpoint exe as the last two arguments, but no difference.

Getting "Exception in thread "main" java.lang.NoSuchMethodError: main"? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main
I am fairly new to Java, and I am unable to figure out why I am getting NoSuchMethodError: main when I execute the following code. I am not sure what does the NoSuchMethodError is pertaining to. It looks like I have everything right. Please help me out here. Thanks a lot.
public class ThreadExample1 extends Thread
{
static String[] msg = {"Java", "programming", "is", "the", "best"};
public ThreadExample1(String id)
{
super(id);
}
#Override
public void run()
{
try
{
Output.displayList(getName(), msg);
}
catch (InterruptedException ex)
{
}
}
}
class Output
{
public static void displayList(String name, String list[]) throws InterruptedException
{
for (int i = 0; i < list.length; i++)
{
Thread.currentThread().sleep((long) (3000 * Math.random()));
System.out.println(name + list[i]);
}
}
public static void main(String[] args)
{
ThreadExample1 thread1 = new ThreadExample1("thread1: ");
ThreadExample1 thread2 = new ThreadExample1("thread2: ");
thread1.start();
thread2.start();
boolean t1IsAlive = true;
boolean t2IsAlive = true;
do
{
if (t1IsAlive && !thread1.isAlive())
{
t1IsAlive = false;
System.out.println("t1 is dead.");
}
if (t2IsAlive && !thread2.isAlive())
{
t2IsAlive = false;
System.out.println("t2 is dead.");
}
}while (t1IsAlive || t2IsAlive);
}
}
I don't have any problem compiling and executing the above code ... Keep in mind that when you want to execute it , you need to use this command line :
java Output
and NOT :
java ThreadExample1
because the main method is within the Output calss and not in ThreadExample1 ...
Save the file as ThreadExample1.java and compile. After that you should run Output class but not the ThreadExample1 class. This is because you have added your main method inside Output class. But since you have made your ThreadExample1.java class public you have to save and compile using that name(javac ThreadExample1.java). After that java Output
Take a look at code-snippet the main() method is in Output class.
Use following command line to launch the Output.main() method:
c:\>java Output
When you do compile a java program you do need to give the file name after javac.
like javac MyProgram.java
and when you do run it using java then you need to mention the name of the class that is having "public static void main(String args[])" method.
Say I have two classes in MyProgram.java : Class First and Class Second
and I have "public static void main(String args[])" in Class Second then I will do the following :
javac MyProgram.java
java Second

Detect whether Java class is loadable

I have two programs: one CLI program, and one GUI. The GUI is a frontend for the CLI, but also a GUI for another program as well.
I am importing the CLI's classes and extending them in the GUI to add GUI elements to the classes, and all is great.
But now I want to split the CLI that I currently have embedded in the GUI (as an included JAR). The JAR is in a fixed location (/opt/program/prog.jar), and the application will only be used on Linux, so I realize that this breaks traditional Java thought.
I've edited the ClassPath in the Manifest file to reflect this change, and it works fine. However, when I remove the file, the GUI fails to load, citing not being able to load the class.
Is there a way to try to load a class and if it does not work, then do something else? In essence, I'm trying to catch the ClassNotFound exception, but have not had any luck yet.
One common way to check for class existence is to just do a Class.forName("my.Class"). You can wrap that with a try/catch that catches ClassNotFoundException and decide what to do. If you want, you could do that in a wrapper class that has a main(). You could try to load the class and if it succeeds, then call main() on the loaded class and if not, do something else.
public static void main(String arg[]) {
try {
Class.forName("my.OtherMain");
// worked, call it
OtherMain.main();
} catch(ClassNotFoundException e) {
// fallback to some other behavior
doOtherThing();
}
}
Is there a way to try to load a class and if it does not work, then do something else?
Assuming you had a class file in C:\ called Foo.class
public static void main(String[] args) {
File f = new File("c:\\");
if (f.exists()) {
URLClassLoader CLoader;
try {
CLoader = new URLClassLoader(new URL[]{f.toURL()});
Class loadedClass = CLoader.loadClass("Foo");
} catch (ClassNotFoundException ex) {
} catch (MalformedURLException ex) {
}
} else {
//do something else...
}
}

Categories

Resources