I have developed a plugin in which a java application is launched.In the console log of this application i want to be notified when new lines added.I have searched internet and found org.eclipse.debug.ui.consoleLineTrackers extention point.I have used it like below.
<extension point="org.eclipse.debug.ui.consoleLineTrackers">
<consoleLineTracker
id="com.plugin.util.MyConsoleTracker"
class="com.plugin.util.MyConsoleTracker"
processType="MyProcessType">
</consoleLineTracker>
</extension>
Then in my java code i have launched application like below.
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classpath);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,false);
config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,MAIN_CLASS_NAME);
config.setAttribute(IProcess.ATTR_PROCESS_TYPE, "MyProcessType");
ILaunch launch = DebugUITools.buildAndLaunch(config, ILaunchManager.DEBUG_MODE,new NullProgressMonitor());
After these, i can launch my application successfully however my class com.plugin.util.MyConsoleTracker is never called.I cant see any error log either.
can anybody please tell me what i am missing here?
For an example you can see how it is made in m2e-core:
http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.launching/src/org/eclipse/m2e/internal/launch/MavenConsoleLineTracker.java
and
http://git.eclipse.org/c/m2e/m2e-core.git/tree/org.eclipse.m2e.launching/plugin.xml
Methed of YourConsoleLineTrackers should be evoked each time when a new line is showed up in console. Inside Line Tracer you have to distinguish to which process this line belongs to.
Marek
Related
I'm working on a project for my Uni where I want to visualize code debugging. For this I somehow need to log the executed Lines of Code and the variables with their values for a given Java program. An example:
public class Main {
public static void main(String[] args){
String abc = "def";
String test = "hello world";
String foo = abc+test;
}
}
If i log this programm my output should be something like this:
Main at line 3:
Main at line 4: abc=def
Main at line 5: abc=def,test = hello world
Main at line 6: abc=def, test = hello world, foo = defhello world
The logging program should run in the background so I can use the logged program normally.
I already tried stuff with Java Agents and Stacktrace but I could'nt get good results. I hope there is any way to do this. Thanks for any help in advance!
There are ways to do this, some IDE like Intelij IDEA actually display the variable value in the editor when you debug.
But if you want to log that, not only the information log would soon become huge (gigabytes/terabytes for real programs) but it would be quite complex.
Here several ways to do this:
Actually use the debugger API to interract with the running program and so log that information: https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/index.html
Make it with a plugin to the compiler so it add the necessary logs. I think that was your approach.
Create a Java => Java compiler that add the matching source and let the standard compiler compile the java. For that there an open source API for eclipse I think that they use for refactoring in the IDE. (Here a blog post that show you can use the API to read java code: https://www.vogella.com/tutorials/EclipseJDT/article.html)
I'm running a test case in debug mode on a new installation of Netbeans 8.2 with a break point on a myId field.
#Test
public void testCreateDocumentSecurityNullRequest() throws Exception {
final Integer myId = 1;
myRequest request = null;
mockMvc.perform(post("/pathTo/apply/" + myId).contentType(contentType).content(json(request)))
.andExpect(jsonPath("$.code", is("400")));
}
I know the test is executed because I see the test results, but the debugger doesn't stop on my break point.
The Debugger Console shows:
Listening on 23206
User program running
LineBreakpoint myTest.java : 'line #' successfully submitted.
User program finished
I don't understand why it's not stopping and just continuing? I tried it in Eclipse and it stops and let's me continue as I would expect. I'm sure I'm just misunderstanding something simple but I read documentation and nothing sticks out to me. I also looked at some other Stack Overflow posts that were similar but those didn't seem to fit my issue.
I had a similar issue. NetBeans 6.5 won't stop on a breakpoint in my code.
I figured that while working with a project I changed source location (was: "project.6.5", I changed it to just "project").
NetBeans remembered the old source location ("project.6.5") which became non-matching as I ran code from "project". A breakpoint was displayed correctly (red square), but won't stop.
I opened Window > Debug > Sources and checked current code ("project"). Breakpoint became a broken square and it wrote in logs about source not matching.
After I unchecked old source ("project.5.6") it started working fine again.
...\AppData\Roaming\NetBeans\8.2\config\Services\org-netbeans-modules-debugger-Settings.properties - remove all 'breakpoint' lines from this file.
Given this code, I am baffled as to why it doesn't print to the console....
engine.compileTemplate(new PrintWriter(System.err));
System.err.flush();
I see nothing on the console. Same problem with System.out too. And yes I verified that the code works by printing to a file successfully.
Try using the constructor with autoflush. You are flushing System.err, not the PrintWriter.
In what context is this code executed?
Really need more info to determine what's going on.
Is your code running in a plugin that you are debugging?
If so, then with "print to the console" - do you mean the console of the same Eclipse instance as your plugin?
For that you would need to do something like:
MessageConsole console = new MessageConsole("My Console", null);
console.activate();
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
MessageConsoleStream consoleStream = console.newMessageStream();
consoleStream.println("Hello, world!");
Otherwise, it goes to the console where Eclipse was launched.
Read more here: http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F
I am trying to save values in a string to a file on my local system by using the org.apache.commons.io.FileUtils static method writeStringToFile.
So, first I downloaded the commons-io-2.4.jar, along with its javadocs and source and imported it into my Eclipse project through the Java Build Path. Everything compiles just fine.
However, when I add the simple line:
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File(Environment.getExternalStorageDirectory().toString() + "/logt.txt"), rslt.toString());
The program crashes. But, it doesn't crash anywhere near that statement. Instead, it crashes at the constructor of the Object which contains the method which calls this function.
In other words
Earlier in my code I create an object TranslateTask,
Which happens to contain a function call doTranslate().
The writeStringToFile call is made within the doTranslate() call,
However the actual crash takes place when I instantiate a TranslateTask object.
But, when I comment out the call to writeStringToFile(), the crash never happens
Even though the crash doesn't take place in the doTranslate() call...
So, just the mere mention of writeStringToFile() makes my program crash when I instantiate an object which contains it.
To make it more eary, I instantiate the object within a Try, catch (RejectedExecutionException e) block but instead the program crashes at this part:
PathClassLoader.findClass(String) line: 243 **Last call in the stack**
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573
PathClassLoader(ClassLoader).loadClass(String) line: 532
Translate$4.run() line: 159 **Crash happens here where I instantiate a TranslateTask object**
So PathClassLoader... Not sure how to approach debugging this. All I know is that if I commend out the org.apache.commons.io.FileUtils.writeStringToFile() call, the error never happens and the code runs fine everywhere.
Running this on the API8/10 of Android, using Eclipse Indigo.
EDIT- Logcat:
08-07 18:40:11.409: I/System.out(1395): debugger has settled (1363)
08-07 18:40:14.399: W/KeyCharacterMap(1395): No keyboard for id 0
08-07 18:40:14.399: W/KeyCharacterMap(1395): Using default keymap:
/system/usr/keychars/qwerty.kcm.bin
Don't think it gives any hint, but this is all that logcat gives after the debugger has settled.
EDIT2 -
For good measure, I just added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But still no go
EDIT3 -
I am starting to think the apache commons-io-2.4.jar doesn't work with Android inherently. Could that be it?
EDIT4 -
In case anyone wants to take a crack at it, here is my code. It is made in Windows 7, Eclipse Indigo. The code is based on the "Hello Android" translate section. All I am trying to do is extract some JSON into a string and save it into my sdcard. But, it has evolved into this mystery here...
https://dl.dropbox.com/u/10790286/Translate.zip (See TranslateTask.java line 111)
EDIT5 -
Interesting update. When I manually put FileUtils.writeStringToFile(new java.io.File("/mnt/sdcard/logt.txt"), "test"); into the eclipse expressions box during a debug, I get the following error
An exception occurred: java.lang.NoSuchMethodError
I don't understand why I would get this error because 1) the WriteStringToFile() function is in the commons-io source. 2) I can instantiate a FileUtils object 3) I imported it into my program.
Whats going on here?
Writing to the path "c:\\temp\\logt.txt" isn't going to work on Android. You can use something like Environment.getExternalStorageDirectory().toString() + "/log.txt" to write to the SD card.
EDIT:
The follow worked on my Galaxy Nexus:
try {
FileUtils.writeStringToFile(new File(Environment.getExternalStorageDirectory(),"log2.txt"), "HelloNow");
} catch (IOException e) {
Log.e(TAG,e.getMessage())
}
Though the programme runs succesfully in complier ,the programme crashes while it runs in android device .Because during the run time it checks over jar files within android dependancies only. I too had similar type of problem .I got it solved when in preferences/properties -> java build .Tick the external jar file .
I am using ANTLRWorks to create ANTLR grammars. I have a valid grammar and the parser and lexer source files are generated as well. I have also tried debugging the generated code and the output is as expected in the debugger output.
But when I try to invoke the __Test__ class generated by the debugger nothing is coming up in the console. I have properly set up the classpath as I can successfully compile the __Test__.java with the same classpath.
What would be the problem? Is there any clear tutorial for writing and compiling a sample parser with antlr and antlrworks?
What do you expect on the console to come up?
Have a look at this project. The ANTLRWorks generated parser is here. As you can see from the dependencies in the POM you need to make sure antlr is in the classpath. Then you use the parser as shown in this class.
final DriftLexer lexer = new DriftLexer(new ANTLRInputStream(inputStream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final DriftParser parser = new DriftParser(tokens);
parser.file();
That should be enough to get your stuff working as well.
ANTLRWorks generates test classes that create a socket connection back to ANTLRWorks, so they aren't usable from the console. You can edit the generated test class to not use the debug port (socket connection) option.
The line to edit is:
FormalSpecParser g = new FormalSpecParser(tokens, 49100, null);
You can change it to:
FormalSpecParser g = new FormalSpecParser(tokens, null);
which uses a debug listener object instead of a port, and the "null" means you're not giving it a debug listener, so debug output is ignored. You could write your own debug listener to print out messages to the console.
See the ANTLR documentation for more information: http://www.antlr.org/api/Java/namespaces.html