How to use system.out.println when using a servlet - java

I want to debug a program by using system.out.println() and what to
check the flow and variables value in eclipse while running a JIRA
plugin which is a Servlet .
When I make some changes to the code in eclipse it reflects on the
browser but when I use system.out.println where should i see the
output ?
Example Code :
#RequiresXsrfCheck
protected String doExecute()
{
LOG.info("BulkCloneDetails doExecute start...");
filterUrl = null;
cloneSubTasks = false;
cloneAttachments = false;
deleteAfterClone = false;
requestId = null;
issues = null;
getServerInfoFromPage();
**System.out.println("Print something here");**
errorsCollection.put("filterPath", i18n.getText("Remote login failed"));
addErrors(errorsCollection);
return SUCCESS;
}

You can put a tail on a file in Terminal so that you can see how it is interacted with. On a webapp I am working on I use the below method.
Username$ cd /Applications/tomcat/apache-tomcat-6.0.16/logs
Then type
tail –f catalina.out
This will then print to your Terminal window. I have had to use this method on a few projects. I advise you to use system.out.println("Name of the File I am printing from") so that if you have several of such then you will know where they are coming from and in what order.

In Eclipse you will be having a number of console options and one would be for Apache Tomcat.
Switch to that console window for observing your messages.
Hope This helps.

Related

Files delete() method in Java is not working

Not able to delete the file001.txt which is created in my local dir path by using the removeFile(s) method in switch case. Then the method removeFile(s) calls the class Employee_Remove, where file.delete() method does not delete the files. Please find the below 2 codes and also screenshot.
I don't want to create duplicate or renaming the filename. I just need to delete the files which is created in my local dir. I did not understanding the problem whether it code problem or Eclipse problem.
Can anyone help to solve the issue.
case 3:
{
System.out.print("\nPlease Enter Employee's ID :");
String s=sc.nextLine();
Employee_Remove epr =new Employee_Remove();
epr.removeFile(s);
break;
}
class Employee_Remove {
public void removeFile(String ID) {
File file = new File("file" + ID + ".txt");
if (file.exists()) {
if (file.delete()) {
System.out.println("\nEmployee has been removed Successfully");
}
} else {
System.out.println("\nEmployee does not exists :( ");
}
}
}
If you get no output to the Eclipse console window, that means that:
Your application code has found the file. If it didn't find it, you would get a message.
The file was not deleted. If the delete had succeeded, then you would get a message to say so.
It is not clear why your application found the file but did not delete it deleted. One possibility is that your application does not have permission to delete the file. A second possibility is that something else has the file open. On Windows that locks the file, and that would prevent the application from deleting it. (There are some other more obscure causes, but I doubt that they would be applicable here ...)
If you want to get diagnostics that explain give more information about why the file wasn't deleted, switch to using the newer java.nio.file.* APIs; e.g. java.nio.file.Files::delete(Path).
If the output is NOT as you described, then it is possible that the application DID delete the file (and said so) but Eclipse has not noticed. In my experience Eclipse does not notice changes made to files that were not made by Eclipse itself.
If this is the case, then doing an Eclipse refresh should update the view. The Eclipse shortcut for this is typically F5 or CNTRL-F5, but check the key bindings.

How to have only one instance of the CHM file opened?

I want to set up only one instance of the CHM file when clicking on "Help" in the menubar and stopping it from opening twice when clicked again - therefore how do I code it?
I've tried to use it with process.isAlive(), but after I close it I want a counter set to zero, which only opens another CHM file when the counter is 0.
helpMenu.addMouseListener(new MouseAdapter() {
// do this after clicked
openCHM();
});
So MouseEvent is fired once.
openCHM() {
Process p;
if(cnt == 0) {
p = Runtime.getRuntime().exec("hh.exe Help.chm");
cnt++;
if(!p.isAlive()) {
cnt = 0;
}
}
I expected the counter to be 0, but then came to the conclusion that MouseEvent already fired once and the code got already executed, therefore it never goes to the second if-statement and sets my counter to 0.
EDIT
There is no correct answer how to open a CHM file once, but there is a workaround that makes it possible, we just need to look if the file is renamable or not.
protected void openCHM() {
try {
File file = new File("YOURNAME.chm");
boolean renamable = file.renameTo(file); // can't rename if file is already open, returns true if renaming is possible
if(renamable) {
Runtime.getRuntime().exec("hh.exe YOURNAME.chm");
} else if(!file.exists() ){
// message: file doesn't exist (in path)
} else {
// file is already open
}
} catch () {
}
}
I'm not a Java programmer but the short story - not possible (AFAIK).
You know, hh.exe is the HTMLHelp executable and associated with *.CHM files. It's just a shell that uses the HTML Help API and is really just hosting a browser window.
HH.EXE is not single instance, if you open a CHM or another file three times using HH.EXE, then three help windows will appear. Try it using PowerShell:
PS D:\_working> hh.exe C:\Windows\Help\htmlhelp.chm
Several client-side command line switches are available to help authors that are part of the HTML Help executable program (hh.exe) and therefore work when HTML Help Workshop is not set up.
KeyHH.exe was running years ago with special parameters.
If you call the HH API directly from your application, and not via a second helper program like HH.EXE or KEYHH.EXE, then you MUST close any open help windows before shutting down the application or you will probably crash Windows.
For some information related to the problem you maybe interested in Open CHM File Once.
Some quoted info from the link above:
When you do that you are just calling the help viewer again and again from the command line, you're not using the HTML Help API which is what you need to access the CHM once it is open. You need to check whether your flavors of Java and Smalltalk support calls to the HTML Help API. This API is documented in detail in the help file of Microsoft HTML Help Workshop, which is the compiler package you installed to be able to generate CHMs.

Cannot search movies in Neo4j Movies sample application

On Ubuntu 16.10 I have followed the instruction from:
https://github.com/neo4j-examples/movies-java-spring-data-neo4j-4
But when I go to http://localhost:8080/ and try to search for e.g. Matrix nothing happens, it just looks empty:
I have verified that the neo4j db has been populated with the required data and I also updated user/pass in the .properties file in the maven project.
Any ideas, and is it possible to find a log somewhere?
Pressing F12 in Firefox gives:
Indicating the error in the sources as described in the below answer, did not manage to get the same error info in Chrome when pressing F12 though.
The code in the GitHub project is not properly updated. The MovieRepository.java class has findByTitleContaining method and #param is missing in this method. If you check the index.html file, javascript code is executing /movies/search/findByTitleLike?title=* URL. The quick fix will be add following method in MovieRepository.java and then execute mvn spring-boot:run command again.
Collection<Movie> findByTitleLike(#Param("title") String title);
In order to have proper source and test classes, you should replace findByTitleContaining method with findByTitleLike method in MovieRepository.java class and also fix the MovieRepositoryTest.java class to use the correct method. Delete testFindByTitleContaining method and add following method in MovieRepositoryTest.java class.
#Test
public void testFindByTitleLike() {
String title = "Matrix";
Collection<Movie> result = instance.findByTitleLike("*"+title+"*");
assertNotNull(result);
assertEquals(1, result.size());
}

Problems with YamlConfiguration.set()

Hey there,
I've got a small problem. I am creating a plugin for Bukkit. I tried to code a in-game config editor - that means that one can change configuration options with a command from inside the game. I already got it to work, but as of the build I'm using (#2879), the method YamlConfiguration.set(String, Object) doesn't seem to work. Here is the essential part of my code for setting and saving the YamlConfiguration I've got.
plugin.debug("option = "+option); // the configuration option
plugin.debug("newvalue = "+value); // the new value
config.set(option, value); // this should set the value of 'option' to 'value'
plugin.debug("savedvalue = "+config.get(option)); // the value saved in the config
As I tested my plugin, I've got the following output.
option = debug
newvalue = false
savedvalue = true
If you need the full and detailed code, look into it on GitHub: GeneralCommands.java, function config(CommandSender, String, String) (line 1074).
Kind regards.
My bad, it was a problem with another method I used (plugin.getConfig()).

Eclipse editor plugin: "ERROR" when opening file outside project

I'm developing an editor plugin for eclipse. It works fine on files within eclipse projects, but when an external file is opened via the "File -> Open File" menu (which works file with, e.g. Java files), I get a page displaying nothing but a horizontal blue line and the word "ERROR". The Error Log of eclipse is empty, as is the log file in the .metadata directory.
What could cause this? How can I diagnose the error when I have no error message that tells me where to look? There doesn't seem to be a way to get more detailed logging from eclipse.
Edit:
I've found that the source of the problem is close to what jamesh mentioned, but not a ClassCastException - there simply is no IDocument instance for the text viewer to display because StorageDocumentProvider.createDocument() returns null. The reason for this is that it only knows how to create documents for instances of org.eclipse.ui.IStorageEditorInput, but in this case it gets an instance of org.eclipse.ui.ide.FileStoreEditorInput, which does not implement that interface, but instead implements org.eclipse.ui.IURIEditorInput
I had the same probleam and finally found solution working for me.
You have to provide 2 different document providers - first extending FileDocumentProvider for files inside your workbench, and second extending TextFileDocumentProvider for other resources outside your workspace. Then you register the right provider acording to the input in your editors doSetInput method like this:
private IDocumentProvider createDocumentProvider(IEditorInput input) {
if(input instanceof IFileEditorInput){
return new XMLTextDocumentProvider();
} else if(input instanceof IStorageEditorInput){
return new XMLFileDocumentProvider();
} else {
return new XMLTextDocumentProvider();
}
}
#Override
protected final void doSetInput(IEditorInput input) throws CoreException {
setDocumentProvider(createDocumentProvider(input));
super.doSetInput(input);
}
then in your new document provider (extending TextFileDocumentProvider) insert somethnig like this:
protected FileInfo createFileInfo(Object element) throws CoreException {
FileInfo info = super.createFileInfo(element);
if(info==null){
info = createEmptyFileInfo();
}
IDocument document = info.fTextFileBuffer.getDocument();
if (document != null) {
/* register your partitioner and other things here
same way as in your fisrt document provider */
}
return info;
}
This works for me :) Finally I have to mention, that I'm not so clever and that I copied this solution from project Amateras (Opensource HTML editor plugin for eclipse)
I'm a little away from the source code at the moment, though I suspect the problem is a ClassCastException:
For a workspace file, the IEditorInput is org.eclipse.ui.IFileEditorInput.
For a local non-workspace file, the IEditorInput is org.eclipse.ui.IStorageEditorInput
The difference is in how you get the contents from the IEditorInput. The JDT does an explicit instanceof check to make the switch.
I don't think that the getAdapter(Class clazz) will return a java.io.InputStream if you offer it.
I don't quite understand why they do it like this, but it feels ugly.
Edit:
A more general point about debugging eclipse apps - it's really very useful to try and assemble all your logs into one place (i.e. the console).
To do this, make sure you use the command line options -console and -consoleLog. The latter has helped save countless hours of time. If you haven't already, learn the most basic things about how to use the console (ss and start are my most often used). This will save some more time diagnosing a certain class of problem.
Did you try creating a JAVA file using the editor, outside the workspace?
When calling the editor with the file path, concat "file://" at the beginning of the file path.e.g: if the path is C://temp//Sample.java, then modify it as file://C://temp//Sample.java.

Categories

Resources