The error:
java.lang.ClassNotFoundException: testprocedure.tp$3
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at core.ProcedureSetup.load(ProcedureSetup.java:57)
at core.Engine.main(Engine.java:25)
I instantiate the object and call the "ProcedureSetup"'s "save" method from Class "tp".
ProcedureSetup ps=new ProcedureSetup(new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }});
ps.save();
however I load from a different collection of programs that has -ALL- required code but "tp"
ProcedureSetup ps=new ProcedureSetup();
ps.load();
Object saving and loading within class:
public void load(){
String path=Operator.persistentGetFile();//gets the file path
ObjectInputStream ois=null;
FileInputStream fin=null;
ProcedureSetup temp=null;
try {
fin = new FileInputStream(path);
ois = new ObjectInputStream(fin);
temp=(ProcedureSetup) ois.readObject();
ois.close();
fin.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(ois!=null){
try {
ois.close();
} catch (IOException e) {}
}
if(fin!=null){
try {
fin.close();
} catch (IOException e) {}
}
if(temp!=null){
a=temp.a;
}else{
load();//If a load is failed, restart process.
}
}
public void save(){
String path=Operator.persistentGetDirectory();//get directory to save to
String input = JOptionPane.showInputDialog(this, "Enter the File name:");
ObjectOutputStream oos=null;
FileOutputStream fon=null;
try {
fon = new FileOutputStream(path+input+".obj");
oos = new ObjectOutputStream(fon);
try {
oos.writeObject(this);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
oos.close();
fon.close();
} catch (IOException e) {
e.printStackTrace();
}
if(oos!=null){
try {
oos.close();
} catch (IOException e) {}
}
if(fon!=null){
try {
fon.close();
} catch (IOException e) {}
}
}
My questions are:
Why are these errors happening?
Why (if necessary) do I need to have "tp" in my classpath?
If there is in fact a way to save the object in its current state with out the necessity of "tp" in the classpath how would I go about doing that? (Links would be lovely)
When you read in a serialized object, Java "reconstitutes" it by using the information in the serialized stream to build a live copy of the object. It can't do this unless it has the .class file for the object's class; it needs a blank copy to "fill out" with the information from the stream.
The best option is usually to make sure that the class is on the class path. If you have some particular reason why this won't work, Java serialization isn't for you; JSON may be a suitable option instead.
new Procedure(){ public void doStuff(){ System.out.println("Stuff is being done"); }}
The above is an anonymous inner class of your tp class. So, to be deserialized, this anonymous inner class, and its enclosing class tp, must be present in the classpath: the stream of bytes contains the name of the class and the fields of the object, but it doesn't contain the byte-code of the class itself.
You should make it a top-level class, or at least a static inner class.
You should also respect the Java naming conventions: classes are CamelCased.
Why are these errors happening?
There is only one error here: java.lang.ClassNotFoundException: testprocedure.tp$3. It means you haven't deployed testprocedure/tp$3.class to the peer.
Why (if necessary) do I need to have "tp" in my classpath?
So that deserialization can succeed. You can't do anything with a class you don't have the .class file for, let alone deserialize instances of it.
Related
Trying to play and audio clip in java, but this error pops up every time. I imported everything I need to so I'm not sure what the issue is.
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream (this.getClass ().getResource ("hopes_and_dreams.wav"));
Clip clip = AudioSystem.getClip ();
clip.open (audioInputStream);
clip.start ();
javax.sound.sampled.LineUnavailableException: Failed to allocate clip data: Requested buffer too large.
at com.sun.media.sound.MixerClip.implOpen(Unknown Source)
at com.sun.media.sound.MixerClip.open(Unknown Source)
at com.sun.media.sound.MixerClip.open(Unknown Source)
at CA_PeterLang.paint(CA_PeterLang.java:828)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The issue of the OP and not being able to find the method is related to the Ready to Program IDE which is apparently running Java 1.4. The .getClip() method in the question was added in Java 1.5 according to the JavaDocs for AudioSystem
However, I have, in the past, had issues where the system would not find my specific speakers, so the following approach has worked for me. Note that I use a URL, but it should be adaptable to a getResource() approach.
private Mixer.Info getSpeakers()
{
Mixer.Info speakers = null;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for (Mixer.Info mi : mixerInfo) {
// System.out.println(mi.getName() + "\t" +
// mi.getDescription());
if (mi.getName().startsWith("Speakers")) {
speakers = mi;
}
}
System.out.println(
(speakers != null ? speakers.getName() : "<no speakers>"));
return speakers;
}
public void playSound(String soundFile)
{
AudioInputStream ais = null;
try {
URL url = new File(soundFile).toURI().toURL();
ais = AudioSystem.getAudioInputStream(url);
Mixer mixer = AudioSystem.getMixer(getSpeakers());
DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
Clip clip = (Clip)mixer.getLine(dataInfo);
clip.open(ais);
clip.start();
do {
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
while (clip.isActive());
}
catch (UnsupportedAudioFileException | IOException |
LineUnavailableException e)
{
e.printStackTrace();
}
}
When called with playSound("Alarm01.wav"), it properly executes. I think this approach uses slightly older methods.
Edit: please do not follow my names here -- they are hacked for testing.
Edit 2: the foreach loop may be changed to:
for (int i = 0; i < mixerInfo.length; ++i) {
Mixer.Info mi = mixerInfo[i];
...
Edit 3: to use as an InputStream rather than a URL, use
InputStream is = this.getClass().getClassLoader().getResourceAsStream(soundName);
// add a check for null
ais = AudioSystem.getAudioInputStream(is);
Edit 4: This method works with Java 1.4 (to the best of my knowledge). I had to hack around on my local machine settings to get the sound, but that is a different issue.
public void playSoundOldJava(String soundFile)
{
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(soundFile);
// TODO: add check for null inputsteam
if (is == null) {
throw new IOException("did not find " + soundFile);
}
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
DataLine.Info dataInfo = new DataLine.Info(Clip.class, ais.getFormat());
if (AudioSystem.isLineSupported(dataInfo)) {
Clip clip = (Clip)AudioSystem.getLine(dataInfo);
System.out.println("open");
clip.open(ais);
clip.start();
do {
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
while (clip.isActive());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
I've never used it but, it seems like you have to do this :
Clip clip = new Clip(); // Think that you can pass the stream as parameter for the builder
clip.open(audioInputStream);
Ref here : https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html#open(javax.sound.sampled.AudioInputStream)
I'm trying to open/save a Text Object from SWT with the ObjectOutputStream. But it doesn't work. Have anybody an idea, why?
public static void read(String fileName, Text textField) {
int c=0;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
c= in.readInt();
textField = (Text) in.readObject();
in.close();
}
catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void write(String fileName, Text textField) {
int c = 1;
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeInt(c);
out.writeObject((Text)textField);
out.close();
}
catch(IOException e){
e.printStackTrace();
}
}
The Error which appears on the console when i save:
java.io.NotSerializableException: org.eclipse.swt.widgets.Text
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at FileIO.write(FileIO.java:42)
at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at Editor.open(Editor.java:230)
at EditorMain.main(EditorMain.java:6)
and this when i try to open the file, what i saved before:
Caused by: java.io.NotSerializableException: org.eclipse.swt.widgets.Text
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at FileIO.write(FileIO.java:42)
at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
... 7 more
You can only use ObjectOutputStream on objects which implement Serializable. The SWT Text class does not implement this.
A SWT Text class contains all sorts of objects which depend on the native UI code, there is no way this can be saved and loaded again.
Hi I am having an error when I try to use the HSSF Workbook. See this error
Exception in thread "Thread-13" java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at digicare.tracking.serial.BulkUpload.UploadProgress$1read2.run(UploadProgress.java:95)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.hssf.usermodel.HSSFWorkbook
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Here's my code:
try {
file = new FileInputStream(new File(FilePath));
try {
workbook = new HSSFWorkbook(file);
} catch (Exception e2){
JOptionPane.showMessageDialog(null, "Error1" + e2.getMessage());
}
//HSSFSheet sheet = workbook.getSheetAt(0);
//HSSFRow row;
//HSSFCell cell;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Error1" + e1.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "Error2 "+ e.getMessage());
}
It seems like whenever I try to use the workbook part it returns an error
Do you have the POI jars in your build path.It says No class found.HSSF is associated with XLS files.
See here for an example in Eclipse.
Depending on which IDE you use, the setup process might vary.
I am currently trying to read an ofx file with java.
But I get the following error: Unhandled exception type FileNotFoundException (for the 2nd line). I am using OFx4j. Could you please give me some tips on that one?
Here is the code I have written so far:
String filename=new String("C:\\file.ofx");
FileInputStream file = new FileInputStream(filename);
NanoXMLOFXReader nano = new NanoXMLOFXReader();
try
{
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
}
Thanks for your comments, I made some changes:
String FILE_TO_READ = "C:\\file.ofx";
try
{
FileInputStream file = new FileInputStream(FILE_TO_READ);
NanoXMLOFXReader nano = new NanoXMLOFXReader();
nano.parse(file);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
System.out.println("Message : "+e.getMessage());
}
catch (Exception e1)
{
System.out.println("Other Message : "+e1.getMessage());
}
But now I am getting this:
Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException
at OfxTest.afficherFichier(OfxTest.java:31)
at OfxTest.main(OfxTest.java:20)
Caused by: java.lang.ClassNotFoundException: net.n3.nanoxml.XMLParseException
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
I am trying to figure it out. I believe it can't find the XMLParseException. But I am not sure.
The second problem that you're encountering: "Exception in thread "main" java.lang.NoClassDefFoundError: net/n3/nanoxml/XMLParseException" means that you haven't included the NanoXML library from here: http://devkix.com/nanoxml.php
You will also need the Apache Commons Logging library, as NanoXML appears to be dependent on this. Available here: http://commons.apache.org/logging/download_logging.cgi
This means that you are not catching FileNotFoundException. Also although this is not relevant to your error message but as best practice you should always close you file stream in the finally block like I have below. There is also no need to do to new String() on the file name either.
Add this catch block for the FileNotFoundException:-
String filename = "C:\\file.ofx";
FileInputStream file = null;
NanoXMLOFXReader nano = null;
try
{
file = new FileInputStream(filename);
nano = new NanoXMLOFXReader();
nano.parse(stream);
System.out.println("woooo It workssss!!!!");
}
catch (OFXParseException e)
{
e.printStackTrace();
throw e;
}catch (FileNotFoundException e){
e.printStackTrace();
throw e;
}finally{
if(file!=null){
file.close();
}
}
I have a Swing Program. I am having trouble to save entire main class to a file.
public class GreenHouseMain extends JFrame implements ActionListener,
MouseListener, Runnable, WindowListener, KeyListener, Serializable
{
//.....other components
static GreenHouseMain ghMain;
}
public static void main(String[] args)
{
ghMain = new GreenHouseMain();
}
public void startEvents()
{
suspended = false;
terminate = false;
jbStart.setEnabled(false);
worker = new Thread(new Runnable()
{
public void run()
{
try
{
//Other Code
} catch (ControllerException e)
{
try
{
Date now = new Date();
String log = "";
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("error.log")));
if (e.getMessage() == "Unknown Windows Malfuction")
{
log = "ErrorCode=1, WindowMalfunction," + now;
} else
{
log = "ErrorCode=2, PowerOut," + now;
}
out.println(log);
jTextArea.append(log + "\n");
out.close();
out.flush();
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream("dump.out"));
//It failed in here, says "java.lang.NullPointerException
output.writeObject(GreenHouseMain.ghMain);
output.flush();
} catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
}
});
worker.start();
}
Few things you should know:
1. All classes have been implements Serializable interface
2. There several threads in the program (don't know if it is reason for exception
3. I have had Serialized a object to file before with about the same code but in a console app. Don't know why it fails here.
at javax.swing.plaf.basic.BasicScrollPaneUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
This
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
should be:
} catch (IOException ex) {
ex.printStackTrace();
}
This will give you much better information, most importantly the exact line where the NullPointerException occurs and from where that line is reached. If that doesn't make the cause of the problem obvious, start the program in a debugger and put a breakpoint on that line.