I want to write a program in java to search a string in entire workspace using eclipse search plugin in my code. I have searched for this problem but couldnt find the result. Kindly help. Every suggestion is appreciated. Thanks in advance.
Asked
Active
Viewed 1,250 times
0
-
Post the code you have tried?If new to the eclipse plugin development then search for tutorials in google – Chandrayya G K Feb 24 '14 at 06:15
-
I tried to search for a string in 1 file using simple java search code. Now I want to search that string in entire workspace. Forget about the plugin part. How should I search for a string in entire workspace? Can any1 provide me code for this problem. Thanks in advance. – brijeshp09 Feb 24 '14 at 07:50
-
Do you want to present the results in the Eclipse `Search` view (like the existing `Search > File Search`) or just do the search in your own code. – greg-449 Feb 24 '14 at 08:15
-
I want to do search in my own code @greg-449. – brijeshp09 Feb 24 '14 at 09:29
-
Use apache lucene file search. search for tutorials – gowtham Feb 24 '14 at 14:16
4 Answers
1
You do realize that there already is such a functionality in Eclipse, do you? In Eclipse Search
-> File...
enter your text, select *.*
as the File name patterns, and workspace
as Scope.
Or do you maybe want to write a plugin that uses this search internally?
From your comments I get the picture that you are looking for code to loop through directories/files and search through them. Maybe this question can help you there.

Community
- 1
- 1

SebastianH
- 2,172
- 1
- 18
- 29
0
Use something like the following to recurse through all the projects and folders for file:
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
// Get all the projects in the workspace
IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
// Search each project
for (IProject project : projects)
{
searchContainer(project);
}
/*
* Search a project or folder.
*/
private void searchContainer(IContainer container)
{
// Get all the resources in the container
IResource [] members = container.members();
// Look at each resource
for (IResource member : members)
{
if (member instanceof IContainer)
{
// Resource is a folder, search that
searchContainer((IContainer)member);
}
else if (member instanceof IFile)
{
// Resource is a file, search that
searchFile((IFile)member);
}
}
}
/*
* Search a file.
*/
private void searchFile(IFile file)
{
// TODO search the file
}
You should run this in a Job
or other background task as it may take some time to run.

greg-449
- 109,219
- 232
- 102
- 145
-
@SebastianH I went through your suggested question n I tried compiling both the code bt was unable to get results. Thanks for your help but can suggest another solution. – brijeshp09 Feb 25 '14 at 06:11
-
I am not able to umderstand your code can u explain it by adding comments in it @Greg-449. Thanks in advance – brijeshp09 Feb 25 '14 at 06:19
-
Thanks Greg-449 for comments but I am confused as what should I declare icontainer,iresource etc if possible can u reveal entire code with explanation it will really grateful of you. Thanks in advance. – brijeshp09 Feb 25 '14 at 11:31
-
I am getting many classloader error. Don't know why. Greg-449 can u help me getting put of this problem I am stuck in this for 2-3 days. If possible provide code for search string. Thanks in advance – brijeshp09 Feb 27 '14 at 09:23
-
Show us your plug-in manifest.mf. You are using `Run > Eclipse Application` to run the plugin? – greg-449 Feb 27 '14 at 09:41
-
-
Guys i have tried doing this program to search string. I didn't got expected results as this program search string among files(eg txt,java) but it doesn't search among subfolders. kindly help. – brijeshp09 Mar 03 '14 at 17:26
-
i have posted my code as answer. kindly comment on it and give your suggestion. thanks in advance. – brijeshp09 Mar 03 '14 at 18:07
0
package search_workspace09;
import java.io.*;
import java.util.regex.*;
public class workspace_search09 {
public static void main(String args[])
{
try
{
String dirName="D:/test_folder";
String stringsearch="world";
//String fileName = "test.txt";
File dir = new File(dirName);
// Open the file c:\test.txt as a buffered reader
File[] dirs=dir.listFiles();
if(dirs!=null)
{
for (int i=0;i<dirs.length;i++)
{
if(dirs[i].isFile())
{
File filename=dirs[i];
System.out.println("Files to search in " +filename);
BufferedReader bf=new BufferedReader(new FileReader(filename));
//System.out.println(filename);
// Start a line count and declare a string to hold our current line.
int countline=0;
String line;
//pattern search
Pattern p = Pattern.compile("\\b"+stringsearch+"\\b", Pattern.CASE_INSENSITIVE);
System.out.println("Search Criteria " +" Filename\t\t\t"+ " Line no. " + " Position\t" + " Line Text ");
while ( (line = bf.readLine()) != null)
{
// Increment the count and find the index of the word
countline++;
Matcher m = p.matcher(line);
// indicate all matches on the line
while(m.find())
{
System.out.println(stringsearch +"\t\t"+ filename+"\t\t" + countline + "\t " + m.start() + "\t"+line);
}
}
//continue;
}
}}}
catch (IOException e)
{
System.out.println("IO Error Occurred: " + e.toString());
}}}

brijeshp09
- 21
- 1
- 1
- 5
0
public class workspace_search14 {
public static void main(String[] args)throws IOException
{
String dir="D:/test_folder";
File root=new File(dir);
findFiles(root,0);
}
public static void findFiles(File root,int depth) throws IOException
{
File[] listOfFiles = root.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
String iName = listOfFiles[i].getName();
if (listOfFiles[i].isFile())
{
if (iName.endsWith(".txt") || iName.endsWith(".TXT")||iName.endsWith(".java"))
{
for (int j = 0; j < depth; j++)
System.out.print("\t");
System.out.println("\nFile_Name: "+iName);
searchFiles(listOfFiles[i]);
}
}
else if (listOfFiles[i].isDirectory())
{
for (int j = 0; j < depth; j++)
System.out.print("\t");
System.out.println("\nDirectory_Name: "+iName);
findFiles(listOfFiles[i], depth+1);
}
}
}
public static void searchFiles(File FileName)throws IOException
{
int countline=0;
String line;
String stringsearch="world";
BufferedReader bf=new BufferedReader(new FileReader(FileName));
Pattern p = Pattern.compile("\\b"+stringsearch+"\\b", Pattern.CASE_INSENSITIVE);
System.out.println("Search Criteria " +" Filename\t\t\t"+ " Line no. " + " Position\t" + " Line Text ");
while ( (line = bf.readLine()) != null)
{
countline++;
Matcher m = p.matcher(line);
while(m.find())
{
System.out.println(stringsearch +"\t\t"+ FileName+"\t\t" + countline + "\t " + m.start() + "\t"+line);
}}}}

brijeshp09
- 21
- 1
- 1
- 5
-
This code works for searching a string in entire workspace. It displays filename,line no.,position,line text. I would like to thank everyone for helping and giving their suggestion. – brijeshp09 Mar 10 '14 at 17:56