How to Use External Class Files in an Eclipse Project - java

My lecturer didn't provide us with the .java files for a tutorial. My question is, how would i use his class files in my eclipse project, and defeat the following error?
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: lec/utils/InputReader
at randomIt.main(randomIt.java:17)
Caused by: java.lang.ClassNotFoundException: lec.utils.InputReader
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Here is my code:
import java.util.Random;
import lec/utils.InputReader;
public class randomIt {
public static void main(String[] args) {
Random generator = new Random();
InputReader myReader = new InputReader();
//Pick a number randomly between 1 and 10!
int number = generator.nextInt(10)+1;
//Ask user to guess...!
System.out.println("Take a guess (1 to 10)");
if (number == myReader.readInt()){
System.out.println("You win");
}
else {
System.out.println("It was " + number + ", tough Luck");
}
}
And here is my Folder Structure:
Random /
*/ bin
* / lec / utils /InputReader
* / src / randomIt.java
Note: his class file is "InputReader.class"

I've had a play with Eclipse to work this one out. Give the following a go:
Create the following directory structure (your desktop will do) classes/lec/utils
Place the InputReader class file in the utils directory.
Remove any references you have to InputReader you currently have in your build path.
Using (right click on project) Properties->Java Build Path->Libraries select the 'Add external class folder' and select the 'classes' folder you created on your desktop and click OK.
Now in the 'Referenced Libraries' in the project folder you should have one called 'classes' and a package path under that called 'lec.utils' which contains the InputReader class.
You can use that class using 'import lec.utils.InputReader' in you own class.
Hope that Helps.

In the project configuration menu, there is a "Build Path->Configure Build Path" menu item. Within that, there is an option to add an "external class folder". Put all the provided class files in a folder, and add that folder to the build path.

You should make the following changes
Modify your randomIt class to have following include line (no lecs/ )
import utils.InputReader
Modify the filename as rnadmIt.java (and not randomit.java) . The class name and fie name has to be exactly same. Also per Sun convention the class should start with a capital letter
$ cd Random
$ javac -classpath ./lec src/randomIt.java

Related

Error using sphinx4 jars without Maven

I have a problem with the API Sphinx4 and I can't figure out why it doesn't work.
I try to write a little class for capture the voice of an user and write his speaking on a file.
1) I have create a new java project on Eclispe.
2) I have create the class TranscriberDemo.
3) I have create a folder "file".
4) I have copy the folder "en-us" and the files "cmudict-en-us.dict", "en-us.lm.dmp", "10001-90210-01803.wav" on the folder "file".
5) I don't use maven, so I have just include the jar files "sphinx4-core-1.0-SNAPSHOT.jar" and "sphinx4-data-1.0-SNAPSHOT.jar".
you can download them here:
core: https://1fichier.com/?f3y6vqupdr
data: https://1fichier.com/?lpzz8jyerv
I know that the source code is available
here: https://github.com/erka/sphinx-java-api
or here: http://sourceforge.net/projects/cmusphinx/files/sphinx4
But I don't use maven so I can't compile them.
My class:
import java.io.InputStream;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
import edu.cmu.sphinx.result.WordResult;
public class TranscriberDemo
{
public static void main(String[] args) throws Exception
{
System.out.println("Loading models...");
Configuration configuration = new Configuration();
// Load model from the jar
configuration.setAcousticModelPath("file:en-us");
configuration.setDictionaryPath("file:cmudict-en-us.dict");
configuration.setLanguageModelPath("file:en-us.lm.dmp");
StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
InputStream stream = TranscriberDemo.class.getResourceAsStream("file:10001-90210-01803.wav");
stream.skip(44);
// Simple recognition with generic model
recognizer.startRecognition(stream);
SpeechResult result;
while ((result = recognizer.getResult()) != null)
{
System.out.format("Hypothesis: %s\n", result.getHypothesis());
System.out.println("List of recognized words and their times:");
for (WordResult r : result.getWords())
{
System.out.println(r);
}
System.out.println("Best 3 hypothesis:");
for (String s : result.getNbest(3))
System.out.println(s);
}
recognizer.stopRecognition();
}
}
My log:
Loading models...
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at edu.cmu.sphinx.util.props.ConfigurationManager.getPropertySheet(ConfigurationManager.java:91)
at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.listAllsPropNames(ConfigurationManagerUtils.java:556)
at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.setProperty(ConfigurationManagerUtils.java:609)
at edu.cmu.sphinx.api.Context.setLocalProperty(Context.java:198)
at edu.cmu.sphinx.api.Context.setAcousticModel(Context.java:88)
at edu.cmu.sphinx.api.Context.<init>(Context.java:61)
at edu.cmu.sphinx.api.Context.<init>(Context.java:44)
at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:37)
at edu.cmu.sphinx.api.StreamSpeechRecognizer.<init>(StreamSpeechRecognizer.java:35)
at TranscriberDemo.main(TranscriberDemo.java:27)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 12 more
Thanks for your help =)
There are multiple issues with your code and your actions:
3) I have create a folder "file".
Not needed
4) I have copy the folder "en-us" and the files "cmudict-en-us.dict", "en-us.lm.dmp", "10001-90210-01803.wav" on the folder "file".
Not needed, you already have models as part of sphinx4-data package.
5) I don't use maven, so I have just include the jar files "sphinx4-core-1.0-SNAPSHOT.jar" and "sphinx4-data-1.0-SNAPSHOT.jar".
This is very wrong because you took outdated jars from unauthorized location. The right place to download jars is listed in tutorial http://oss.sonatype.org
https://oss.sonatype.org/service/local/repositories/snapshots/content/edu/cmu/sphinx/sphinx4-core/1.0-SNAPSHOT/sphinx4-core-1.0-20150223.210646-7.jar
https://oss.sonatype.org/service/local/repositories/snapshots/content/edu/cmu/sphinx/sphinx4-data/1.0-SNAPSHOT/sphinx4-data-1.0-20150223.210601-7.jar
You took malicious jars from some random website which might have a virus or rootkit in them.
here: https://github.com/erka/sphinx-java-api
This is a wrong link too. The correct link is http://github.com/cmusphinx/sphinx4
InputStream stream = TranscriberDemo.class.getResourceAsStream("file:10001-90210-01803.wav");
Here you use file: URL scheme which points to files in inappropriate context. If you want to create InputStream from file do like this:
InputStream stream = new FileInputStream(new File("10001-90210-01803.wav"));
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
This error is caused by the fact you took a jar from other place and it said you need additional dependencies. When you see ClassDefFoundError it means you need to add additional jar into your classpath. With official sphinx4 you should not see this error.
Solved.
In fact it was a silly mistake...
Thank you #Nikolay for your answer. I already accept your answer but I resume the process here:
1) Download the sphinx4-core and sphinx4-data jars from https://oss.sonatype.org/#nexus-search;quick~sphinx4.
2) Include them in your project.
3) Test your code.
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
public class SpeechToText
{
public static void main(String[] args) throws Exception
{
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.dmp");
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
SpeechResult result;
while ((result = recognizer.getResult()) != null)
{
System.out.println(result.getHypothesis());
}
recognizer.stopRecognition();
}
}
And that is all!
If you need the source code of Sphinx4: https://github.com/cmusphinx/sphinx4

LWJGL Controllers in IntelliJ

Every time I try to build a basic example program for using LWJGL controllers, it crashes. Here's the code:
package com.czipperz.blogspot.tests.controllers;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;
/**
* Created by Chris on 10/23/2014.
*/
public class Main {
//left joystick x and y
private float x, y,
//right joystick x and y. Like the commas?
rx, ry;
//This Controller object MUST BE org.lwjgl.input.Controller NOT net.java.games.input.Controller
private Controller c;
private boolean start;
public static final int BUTTON_A = 1, BUTTON_B = 2, BUTTON_X = 3, BUTTON_Y = 4,
BUTTON_LEFT_BUMPER = 5, BUTTON_RIGHT_BUMPER = 6, BUTTON_SELECT = 7, BUTTON_START = 8,
//When you PRESS the joysticks (click)
BUTTON_LEFT_JOYSTICK = 9, BUTTON_RIGHT_JOYSTICK = 10;
public static void main(String[] args) {
new Main();
}
public Main() {
//Says, find controllers and store them in the Controllers class to JInput. Dont worry bout it.
try {
Controllers.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
// Gets you a shiny "new" controller instance.
// Think of index like you would think of an array.
c = Controllers.getController(0);
//Sets "deadband"
float dead = .06f;
c.setXAxisDeadZone(dead);
c.setYAxisDeadZone(dead);
c.setRXAxisDeadZone(dead);
c.setRYAxisDeadZone(dead);
//Using the controller requires you to call the poll() function to receive new data.
c.poll();
x = c.getXAxisValue();
y = c.getYAxisValue();
rx = c.getRXAxisValue();
ry = c.getRYAxisDeadZone();
//NOTE: getZAxisValue(), getRZAxisValue(), getZAxisDeadZone(), getRZAxisDeadZone() DO NOT WORK
//DPAD = POV
float povx = c.getPovX();
float povy = c.getPovY();
boolean lbumper = c.isButtonPressed(BUTTON_LEFT_BUMPER);
}
}
The problem is that every time I build, this is the log: (Sorry I just can't hit space that many times)
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains IntelliJ IDEA Community Edition 13.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Users\Chris\workspace\Controllers\out\production\Controllers;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lzma.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lwjgl.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\jinput.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lwjgl_test.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lwjgl_util.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lwjgl-debug.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\asm-debug-all.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\lwjgl_util_applet.jar;C:\Users\Chris\workspace\lwjgl-2.9.1\jar\AppleJavaExtensions.jar;C:\Program Files (x86)\JetBrains IntelliJ IDEA Community Edition 13.1.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.czipperz.blogspot.tests.controllers.Main
WARNING: Found unknown Windows version: Windows 8.1
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
java.lang.UnsatisfiedLinkError: no jinput-dx8_64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at net.java.games.input.DirectInputEnvironmentPlugin$1.run(DirectInputEnvironmentPlugin.java:75)
at java.security.AccessController.doPrivileged(Native Method)
at net.java.games.input.DirectInputEnvironmentPlugin.loadLibrary(DirectInputEnvironmentPlugin.java:67)
at net.java.games.input.DirectInputEnvironmentPlugin.<clinit>(DirectInputEnvironmentPlugin.java:109)
at net.java.games.input.DirectAndRawInputEnvironmentPlugin.<init>(DirectAndRawInputEnvironmentPlugin.java:45)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:438)
at net.java.games.input.DefaultControllerEnvironment.getControllers(DefaultControllerEnvironment.java:157)
at org.lwjgl.input.Controllers.create(Controllers.java:71)
at com.czipperz.blogspot.tests.controllers.Main.<init>(Main.java:32)
at com.czipperz.blogspot.tests.controllers.Main.main(Main.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
java.lang.UnsatisfiedLinkError: no jinput-raw_64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at net.java.games.input.RawInputEnvironmentPlugin$1.run(RawInputEnvironmentPlugin.java:75)
at java.security.AccessController.doPrivileged(Native Method)
at net.java.games.input.RawInputEnvironmentPlugin.loadLibrary(RawInputEnvironmentPlugin.java:67)
at net.java.games.input.RawInputEnvironmentPlugin.<clinit>(RawInputEnvironmentPlugin.java:109)
at net.java.games.input.DirectAndRawInputEnvironmentPlugin.<init> (DirectAndRawInputEnvironmentPlugin.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:438)
at net.java.games.input.DefaultControllerEnvironment.getControllers(DefaultControllerEnvironment.java:157)
at org.lwjgl.input.Controllers.create(Controllers.java:71)
at com.czipperz.blogspot.tests.controllers.Main.<init>(Main.java:32)
at com.czipperz.blogspot.tests.controllers.Main.main(Main.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at org.lwjgl.input.Controllers.getController(Controllers.java:116)
at com.czipperz.blogspot.tests.controllers.Main.<init>(Main.java:38)
at com.czipperz.blogspot.tests.controllers.Main.main(Main.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
net.java.games.input.DirectAndRawInputEnvironmentPlugin is not supported
Process finished with exit code 1
I know this thread is old, but for people who are searching for the solution.
The answer is, you have to include the native libs to your project. For the use of jinput.jar you have to include following libs:
jinput-dx8.dll
jinput-dx8_64.dll
jinput-raw.dll
jinput-raw_64.dll
You can download the natives here DOWNLOAD.
You will get a jar-file. Unzip the file with winrar to get the dll-files.
Now go to project and open the context menu with a click on the right mouse button -> Build Path -> Configure Build Path -> Choose the Libraries tab -> Click on the little arrow next to the jinput.jar to expand the view -> Make a double click with the left mouse button on Native library location -> Choose the directory of the dll-files.
Confirm the dialogs and try to run your program again.
In LWJGL there is a "native/" folder containing the respective native libraries for each platform/OS. Just choose your platform and set java.library.path to that directory.
This is your problem.
First Step:
Create a folder and rename it to LWGJL_New then in that folder create two other folders and rename the first on libs and the second one natives.
Now go to the LWGJL library that you have downloaded. Copy the jar files to LWGJL_New/libs. Then go to the LWGJL library that you have downloaded, in it, there is a folder containing respective native libraries for each platform/OS.
Choose one of them that you need and copy it to LWGJL_New/natives.
Second step:
Go to the module settings of your project(F4) then click library.Click add sign(+).Click on java. Now find the LWGJL_New folder and in it click on the jar folder and click ok. Then Click on Apply and ok.
Third step:
Now if you run your project you will occur that problem, So go to the Edit Configuration part
Then in VM options write this:
-Djava.library.path="***"
Instead of star signs write the address of the native folder that we have just created.
For example ->
D:\LWGJL_New\natives\windwos
D:\LWGJL_New\natives\linux
Then click on OK.
Then run your project.

problems separating class and source files

In my _Mathematics package. I've separated the source files into bin and src folders like so:
_Mathematics ->
Formulas ->
src ->
// source files containing mathematical formulas...
// Factorial.java
bin ->
// Factorial.class
// class files containing mathematical formulas...
Problems ->
src ->
// Permutation.java
// source files containing mathematical problems...
bin ->
// Permutation.class
// class files containing mathematical problems...
But, when I compile the file with main(), there is an error like so:
Exception in thread "main" java.lang.NoClassDefFoundError: _Mathematics\Problems
\bin\Permutations (wrong name: _Mathematics/Problems/bin/Permutations)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Here's the Permutation.java file, where main() is located.
package _Mathematics.Problems.bin;
import _Mathematics.Formulas.bin.Factorial;
public class Permutations {
public static void main(String args[]) {
System.out.printf("There are 10 students. Five are to be chosen and seated in a row for a picture.%nHow many linear arrangements are possible?%n" +
(new Factorial(10).r/new Factorial(5).r) + "%n%n");
System.out.printf("How many permutations are there in the word 'permutation'?%n" +
new Factorial(11).r + "%n%n");
}
}
And here is the other file I have, Factorial.java:
package _Mathematics.Formulas.bin;
public class Factorial {
public int o;
public long r;
public Factorial(int num) {
long result = 1;
for(int i = num; i > 0; i--)
result *= i;
this.o = num;
this.r = result;
}
}
Should I keep the package _Mathematics.Problems.bin;, or should I change it to package _Mathematics.Problems.src;?
What is wrong with my code??
Help would be much appreciated.
Two issues worth mentioning:
bin directories are normally used for executable files. This is because (generally) your OS will have an environment setting that points to these directories, so when you try to run a program, it knows where to look. When you run a Java program, Java itself is the executable (your OS needs to know where to find it). The OS doesn't need to find your actual Java class files, Java needs to find them, for which it uses a completely different environment setting (the classpath). Because of this, if you're putting Java class files in a bin directory, you're probably doing something wrong.
Secondly, your package structure (_Mathematics.Problems.bin) should match exactly the directory structure, but it should reflect the purpose of the classes, so _Mathematics and Problems are reasonable parts of a package structure, but, again, bin or src, is not. Normally, I would create classes and src directories and then my package structure begins under there
So, as explained above, to fix the issue:
make sure the directory and package structures are identical for
your src and classes
by removing the bin part of your package structure, this will be
easier.
For class files, you need to maintain the folder structure which your program is expecting
_Mathematics\Problems\bin\Permutations

Strange class files appearing in my bin folder

I am using Eclipse + Window Builder + few third party libraries to build a gui application,
Entry point for my application resides in MainWindow.java file, which also contains the gui of the application, other than that I have few helper classes.
My application works fine, but when I checked into bin folder I find four more class files there namely:
1. MainWindow$1.class
2. MainWindow$2.class
3. MainWindow$3.class
4. MainWindow$4.class.
Now I don't understand why these files are here, When I deleted these files, Eclipse throws an exception
Exception in thread "main" java.lang.NoClassDefFoundError: gui/MainWindow$1
at gui.MainWindow.main(MainWindow.java:71)
Caused by: java.lang.ClassNotFoundException: gui.MainWindow$1
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
the code that throws the exception is:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frmMailExtractor.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
I am not able to find out why these files are there and who put them? any ideas?
OK so these are the anonymous classes, but why the Exception then?...and why can't I see them in my eclipse..becasue when I export it as a runnable jar, the jar throws the same exception
These are class files generated for anonymous inner classes inside MainWindow. It probably contains a bunch of event listeners implemented as such.
Anonymous inner classes get compiled into separate class files with the name <OuterClass>$<nnn>.class, where <nnn> is a compiler-generated number.
Those are what anonymous classes get compiled into. The number in the filename is the ordinal of the related anonymous class in the respective Java compilation unit.

How can I use Akka Actors in a Java application?

I would like to use Akka actors in Java.
I downloaded the akka-1.0.zip and added akka-actor-1.0.jar to my "Build Path" in Eclipse.
Then I wrote this Actor class:
package com.example;
import akka.actor.UntypedActor;
public class MyActor extends UntypedActor {
public void onReceive(Object message) throws IllegalArgumentException {
if (message instanceof String) {
System.out.println("Received: " + message);
} else throw new IllegalArgumentException("Unknown message: " + message);
}
}
But I get errors in Eclipse:
The type scala.Option cannot be resolved.
The type scala.Some cannot be resolved.
The type scala.PartialFunction cannot be resolved.
The type scala.ScalaObject cannot be resoled.
Do I need to add any more files to my "Build Path" or what am I doing wrong? I don't find the documentation beeing that helpful.
Update: I added scala-library.jar to my Build Path and the above erros disappeared. But I get an error when I compile and run the application:
Exception in thread "main" java.lang.NoClassDefFoundError: net/lag/configgy/ConfigMap
at akka.actor.Actors.actorOf(Actors.java:70)
at com.example.ActorTest.main(ActorTest.java:9)
Caused by: java.lang.ClassNotFoundException: net.lag.configgy.ConfigMap
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Here is the main class where I use my actor:
package com.example;
import akka.actor.ActorRef;
import akka.actor.Actors;
public class ActorTest {
public static void main(String[] args) {
ActorRef myActor = Actors.actorOf(MyActor.class);
myActor.start();
System.out.println("My Actor started");
}
}
In your akka-1.0.zip file there is scala-library.jar. Try adding it to the build path.
Also, there is a lib_managed directory inside the zip, which contains further library files. Possibly aslo some of them will be needed.
To avoid this kind of situations you should try maven. There is a Akka repository: http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/
You can find a complete, working example here; it's a Maven project, so it will get the dependencies for you automatically.
I see this problem using Akka in a Java project in Eclipse with the Scala IDE installed. The problem goes away when using an Eclipse instance without the Scala IDE installed. Eclipse is perhaps confused by two Scala libraries around?
I don't know Akka actors but it seems you need some scala support (libs on the build path) as well.

Categories