This is gonna be a big mess to explain, but if anyone has any pointer, Please share!
From an applet I'm calling a 3rd parity lib which uses JNI do fetch some info in an OS specific manner. I don't know what it calls since I don't have the code and support isn't very responsive.
The thing is, all works well on windows (not just my machine) but on Mac OS one of the lib's methods throws a lib specific exception (a code and a "Cannot complete" ~message). It seems to be a security related issue since the method works when I call it from within init().
The jar with my applet is self-signed, the library has 4 more jars that are signed by the vendor. The lib's method is called from within an "AccessController.doPrivileged" block as an applet method that is called from JavaScript - and this all works, but on windows only.
On Mac this call from JavaScript to the applet method that calls the lib's method gets the exception.
Here is what I tried:
I moved the lib's method call in init() just for the test and it works ok, only without an "AccessController.doPrivileged" block.
I tried starting a Thread in init() (in start() as well):
- using a timer to have the Thread calls the lib's method every 10sec works fine and I can get the updated reply troughs a String buffer - this isn't a preferred solution.
- but if I use a flag in the thread so that the applet method can return the result it throws the same exception.
Here is the code in my applet's method:
checkRunner.refreshWindowsList = true;
while (checkRunner.refreshWindowsList) {
try {
Thread.sleep(300);
} catch (Exception ex) {
System.out.println("Ignoring exception: " + ex.getMessage());
}
}
return checkRunner.windowsTitles;
and in the Thread's run method:
try {
while (true) {
if (refreshWindowsList) {
windowsTitles = getWindowsTitlesPrivileged();
//windowsTitles = getWindowsTitles();
refreshWindowsList = false;
}
Thread.sleep(300);
}
} catch (Exception ex) {
System.out.println("Ignoring exception: " + ex.getMessage());
}
I don't understand how just passing a flag can cause the different results.
Furthermore, it seems that when the applet is created from JavaScript code all of it is sandboxed and the lib's method throws the exception even from init() ... again only on Mac OS.
If anyone made it this far - Thank You : )
To answer my question, even thought I highly doubt this is a common problem...
It turns out that the issue is not security related after all. The problem originates because of a quite inexplicable (to me) behaviour of the 3rd parity library - whenever the problematic method is called, even in a separate thread, and in the main thread their is a loop to wait for the answer - the exception is thrown.
So the following code fails, but when the sleep line at the end is removed it works ok. This happens no matter where the code is, init() or a method called from JavaScript - the behaviour is the same.
new Thread(new Runnable() {
public void run() {
AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
callTheMehod();
}
});
}
}).start();
try { Thread.sleep(5000); } catch (exce....) {}
I ended up splitting the code into two methods called from JavaScript - one that calls the library method, putting the result in a volatile String and another that simply returns this volatile String. In JavaScript their is a waiting loop between the two. Strange thing is that if this loop has an interval that is very short - and the method to check for the result is called too often the library method fails again.
So it seems whenever there is too much load on the main thread the exception is thrown. This is quite inexplicable to me but since the library is using JNI I guess is a lot that could go wrong.
Related
I've written a small method that is meant to tell me if another instance of the application is already running. I am aware that there are many ways to find out if another instance is running, but I chose this one. I am creating an empty file and keeping it locked for the duration of the application instance. If another instance is running, the tryLock() method is supposed to return null:
private static boolean alreadyRunning() throws IOException {
FileChannel fc = FileChannel.open(MYLOCKFILE,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.DELETE_ON_CLOSE);
return fc.tryLock() == null;
}
(MYLOCKFILE is a Path for a file in my temp directory.)
When testing this on Windows 7 Professional 64-bit, I found that it works as expected for the first instance and the second attempted instance. However, after the second instance exits (leaving just the first instance running), when a third instance is run, the tryLock() call throws java.nio.file.AccessDeniedException instead of returning null. Can you explain this behaviour? If this is considered normal behaviour, how can I differentiate between an existing instance having the file locked, and a real 'access denied' situation such as an idiot setting the TEMP directory to read-only?
I made a test project and tested the code the only problem because of which java.nio.file.AccessDeniedException is thrown is StandardOpenOption.DELETE_ON_CLOSE option used in the code.
I removed the option and it works fine now
FileChannel fc = FileChannel.open(MYLOCKFILE, StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
Explanation that I can think because of which java.nio.file.AccessDeniedException is thrown is that as soon as your second instance terminates the option StandardOpenOption.DELETE_ON_CLOSE [More explaination] will attempt to delete the file on JVM exit and failing might have registered an event in kernel or OS to delete the file as and when possible. So if any other process tries to access, create or write the same file before deletion it throws java.nio.file.AccessDeniedException as a delete operation is already pending for that file.
EDIT
As per your new comment, you can add the following code in try finally block placed after checking alreadyRunning() code.
Snippet Example:
if(!alreadyRunning())
{
try
{
// YOUR CODE THAT RUNS
while(true)
{
//YOUR
Thread.sleep(35000);
}
}
finally
{
new File("f:\\test.lock").deleteOnExit();
}
}
I have a static method used to get the title from a PDF using the metadata via itext, which is used as a small part of a major Task.
I noticed an inexplicable path that I narrowed down to this section of code. Specifically, in the line where I instantiate a PdfReader, the process doesn't throw an exception or continue through to the print statement. In fact, it clears out all of my for loops up to the top level of my program and acts as if nothing has happened and my task completed.
try {
System.out.println("Entered method");
PdfReader myReader = new PdfReader(file.getAbsolutePath());
System.out.println("Reader instantiated"); //if no issues, prints to console
Map<String, String> info = myReader.getInfo();
System.out.println(info.get("Title"));
return info.get("Title");
} catch (IOException e) {
System.out.println("PdfReader throws exception"); //if issues, prints to console
e.printStackTrace();
}
Unless I'm mistaken, when this set of code is executed in my method, either "Reader Instantiated" or "PdfReader throws exception" is printed out to the console.
Neither happens. Instead, the process skips every if/for/while loop it is currently in and ends the task.
I'm wondering if someone can explain to me what is happening and how I should go about fixing it?
In the odd event this is searched for, yes, catching Throwable stops the thread from bailing out. I had never seen something like this before. The cause behind the problem was that a PDF was password-protected, so getInfo() failed.
I am having some strange trouble with the method await(Future future) of the Controller.
Whenever I add an await line anywhere in my code, some GenericModels which have nothing to do with where I placed await, start loading incorrectly and I can not access to any of their attributes.
The wierdest thing is that if I change something in another completely different java file anywhere in the project, play will try to recompile I guess and in that moment it starts working perfectly, until I clean tmp again.
When you use await in a controller it does bytecode enhancement to break a single method into two threads. This is pretty cool, but definitely one of the 'black magic' tricks of Play1. But, this is one place where Play often acts weird and requires a restart (or as you found, some code changing) - the other place it can act strange is when you change a Model class.
http://www.playframework.com/documentation/1.2.5/asynchronous#SuspendingHTTPrequests
To make it easier to deal with asynchronous code we have introduced
continuations. Continuations allow your code to be suspended and
resumed transparently. So you write your code in a very imperative
way, as:
public static void computeSomething() {
Promise delayedResult = veryLongComputation(…);
String result = await(delayedResult);
render(result); }
In fact here, your code will be executed in 2 steps, in 2 different hreads. But as you see it, it’s very
transparent for your application code.
Using await(…) and continuations, you could write a loop:
public static void loopWithoutBlocking() {
for(int i=0; i<=10; i++) {
Logger.info(i);
await("1s");
}
renderText("Loop finished"); }
And using only 1 thread (which is the default in development mode) to process requests, Play is able to
run concurrently these loops for several requests at the same time.
To respond to your comment:
public static void generatePDF(Long reportId) {
Promise<InputStream> pdf = new ReportAsPDFJob(report).now();
InputStream pdfStream = await(pdf);
renderBinary(pdfStream);
and ReportAsPDFJob is simply a play Job class with doJobWithResult overridden - so it returns the object. See http://www.playframework.com/documentation/1.2.5/jobs for more on jobs.
Calling job.now() returns a future/promise, which you can use like this: await(job.now())
I have multiple Jasper Reports (with sub-reports) throughout my application. For some reason, one report (that also contains sub-reports) isn't working anymore. After debugging more than 1 day, I found out that it enters an infinite loop and keeps creating Threads for sub-report filling.
Debugger keeps looping between:
JRSubReportRunnable.java
public void run()
{
running = true;
error = null;
try
{
fillSubreport.fillSubreport();
}
catch (JRFillInterruptedException e)
{
//If the subreport filler was interrupted, we should remain silent
}
// we have to catch Throwable, because it is difficult to say what would happen with the master
// filler thread in case we don't
catch (Throwable t) //NOPMD
{
error = t;
}
running = false;
}
The above method starts a Thread in order to fill a sub-report. Once done, sets running = false and the debugger gets to:
JRThreadSubreportRunner.java
public void run()
{
super.run();
if (log.isDebugEnabled())
{
log.debug("Fill " + subreportFiller.fillerId + ": notifying of completion");
}
synchronized (subreportFiller)
{
//main filler notified that the subreport has finished
subreportFiller.notifyAll();
}
}
Once the thread finishes, it gets to the above's method subreportFiller.notifyAll(); line. Then, the debugger goes back to JRSubreportRunnable.java, and so on.
Theoretically, if I have 5 sub-reports, it should create 5 threads (works for me for other reports). Unfortunately, for this case, it keeps creating threads, and my debugger gets "stuck" between the 2 methods mentioned above (FYI: the classes are from the jasperreports-3.7.6-sources.jar).
Also tried:
I found a similar StackOverflow question, but the answer proposed there did not work for me. Neither did any of the proposed solutions from this thread on the JasperSoft Community.
I really cannot figure why this issue appears. I am sure it is something minor as it used to work. Hopefully someone else stumbled upon this and might have a solution. Thanks in advance for any answer. (I know I haven't provided really much info about the content of my sub-reports, but it is pretty private; nevertheless, I can assure you that the contents of the report and associated sub-reports did not change - checked with Git)
I had the exact same problem and solved it by changing the isPrintWhenDetailOverflows property of my subreport from true to false
as suggested here:
http://community.jaspersoft.com/questions/527078/infinite-loop-subreport-fill
hope it helps
ANSWER:
If you ever see these lines and are mistified like I was, here's what they mean.
Thread[AWT-EventQueue-0] (Suspended (exception NullPointerException))
EventDispatchTread.run() line: not available [local variables unavailable]
It's not that the variables are unavailable because they are lurking behind a shroud of mystery in a library somewhere dank. No no, they just went out of scope! It's still your fault, you still have to find the null, and no you can't blame the library. Important lesson!
QUESTION:
One of the most frustrating things for me, as a beginner is libraries! It's a love/hate relationship: On the one hand they let me do things I wouldn't normally understand how to do with the code that I do understand, on the other hand because I don't completely understand them, they sometimes throw a wrench in code that is otherwise working fine! It's because I don't understand the errors that can occur when using these libraries, because I didn't write them, and because eclipse doesn't give me a great deal to go with when one of imports starts acting up...
So here's the problem: I've been working with java.awt.event to handle a bunch of JButtons on the screen for this and that. I get an error when I use one of the buttons I've made. The error is:
Thread[AWT-EventQueue-0] (Suspended (exception NullPointerException))
EventDispatchTread.run() line: not available [local variables unavailable]
What does this mean? What could be causing it? I'm embarrassed to post code, but if you can stand to try to decipher my terrible style, here is the method that seems to cause this error to be thrown.
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String name;
code...
if(cmd.equals("Play")) {
name = field.getText();
card = getCard(name);
if(card != null) {
if(rules.zoneHasCard(card, rules.hand)) {
display.updateStatusMessage(rules.play(card));
field.setText("");
display.updateHand(rules.zoneList("hand"));
display.updateDiscard(rules.zoneList("Discard")); // This is the error here! The discard Zone was empty!
}
else {
field.setText("You do not have " + card.getName());
field.selectAll();
}
}
else {
field.setText("That cardname is unused");
field.selectAll();
}
}
}
Welcome to the complexity of writing GUI code.
When you run a Swing program, a background thread called the Event Dispatch Thread is created. When the user clicks on a JButton, for example, JButton creates and fires an event using this Event Dispatch Thread. Hence the name: it's the thread that dispatches events!
Your code:
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
String name;
// more code...
}
is called by this Event Dispatch Thread, so your code can handle the event.
Somewhere within your code you are trying to do something with a variable that is currently equal to null. The error message is telling you, "hey while running some code on the event dispatch thread, I encountered a NullPointerException" in your code.
Why are you not receiving more info? Possibly you configured Eclipse not to include debug info when compiling?
For now, I recommend adding some lines to your actionPerformed method to show the state of variables:
System.out.println("field = " + field);
System.out.println("rules = " + rules);
System.out.println("display = " + display);
See if this shows you any nulls.
Even if the NullPointerException comes from a library, the stack trace will show which line of your code called that library. But only if you've configured Eclipse to generate debugging info.
In the longer term, work through the Sun's Swing Tutorial to learn more about these issues.
Any method call on a null object will raise a null pointer exception.
In your code, rules, name or display could be null and cause an exception.
Use a debugger (such as the one included in the eclipse IDE) and set a breakpoint at the start of the actionPerformed() method, then step through it line by line to see when a variable you try to invoke a method on is null.
Just don't stop reading the stack trace after two lines. Somewhere in the stack trace you'll recognise the name of one of the classes/methods which you did write. Start looking there. (btw, people spend way to much time inside debuggers :-))
You might have forgotten to actually set an ActionCommand.
In the ActionEvent API Doc there's a note regarding possible null results of getActionCommand().