I am working on eclipse plugin in which i have to open a file from project explorer.
Suppose i have a project ABC in project explorer. after right click on project i got a option to run my plugin tool. after processing i got some result like Check file xyz.java.
Now i want to open this file in IDE by code
i am using this
File absolute = new File("/Decider.java");
File file = new File("/Decider.java");
IFileStore fileOnLocalDisk = EFS.getLocalFileSystem().getStore(absolute.toURI() );
FileStoreEditorInput editorInput = new FileStoreEditorInput(fileOnLocalDisk);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
page.openEditor(editorInput, "org.eclipse.ui.DefaultTextEditor");
page.openEditor(editorInput, "MyEditor.editor");
IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI() );
IDE.openEditorOnFileStore( page, fileStore );
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
IPath path = new Path(" /DirectoryReader.java");
IFile sampleFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
IEditorInput editorInput1 = new FileEditorInput(sampleFile);
IWorkbenchWindow window1=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page1 = window1.getActivePage();
try {
page1.openEditor(editorInput1, "org.eclipse.ui.DefaultTextEdtior");
} catch (PartInitException e1) {
e1.printStackTrace();
}
here it's create a new file named decider in c drive which means it's getting a wrong path.
but when i use path code in some independent java file as a normal JAVA project it's getting the correct path.
For a file in the workspace you should use IFile. If you have a selection from Project Explorer or another view that should already be an IFile or can be adapted to an IFile.
If you just have a workspace relative path use ResourcesPlugin.getWorkspace().getRoot().getFile(path) (path would include a project).
To open the default editor for the file contents use
IDE.openEditor(page, file, true);
to open a specific editor use
IDE.openEditor(page, file, "editor id");
IDE is org.eclipse.ui.ide.IDE.
Related
I need to create a report system using LaTex templates and Java like programming language. I use JLR library but it's freeeware, this is my code:
File workingDirectory = new File("./config/output");
File desktop = new File("./config/desktop");
File invoice1 = new File("./config/templates/template1.tex");
File invoice2 = new File("./config/templates/template2.tex");
JLRGenerator pdfGen = new JLRGenerator();
pdfGen.deleteTempTexFile(false);
if (!pdfGen.generate(invoice1, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
if (!pdfGen.generate(invoice2, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
Searching into the Web I find JLatexMath but, as far as I know, only generates equations in LaTex and not the entire PDF. Do you know a library in Java which generates an entire PDF file using a LaTex template?
Update: I execute de .tex file using Runtime.getRuntime().exec("pdflatex.exe...") command. But I don't achive save the PDF file.
Thanks in advance
I think I have the solution, here is the code:
public void generateReport()
{
Process p;
try {
p = Runtime.getRuntime().exec("C:\\pdflatex.exe -synctex=1 -interaction=nonstopmode ./config/log/document.tex");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It generates a .PDF and .dvi file in the same place of your .tex file. Thank you much for your help :)
I have a configuration file from which I have to read some properties. This configuration file exists in diferent locations but has the same name. I am able to read a configuration file, but only one from my project. I used the following code:
Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
try {
prop.load(inputStream);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (inputStream == null) {
try {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have to open the config file using the full path so instead of propFileName="config.properties" to insert the absolute path to the config file. The config file can't be opened if I use the absolute path.
How can this be done ?
InputStream inputStream = new FileInputStream("path");
will open a file with an absolute path.
Note: This will only work for a file, it will not work for anything included in a plugin jar.
You can use FileInputStream for locating config file outside of your project path.
Properties prop = new Properties();
InputStream input = new FileInputStream("/home/ubuntu/Desktop/sample.properties");
prop.load(input);
System.out.println(prop.get("test"));
When specifying path we need give file name with extension.
I have the problem, that I create a new file in a Java program, but I always get an exception, that the new created file is not local, when I try to open it on the eclipse project explorer view.
The code where I create it is as follows:
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject project = workspaceRoot.getProject(projectName);
FileUtil myFile = new FileUtil();
if (!project.getFile(FILE_NAME).exists()) {
IFile newFile = project.getFile("conf.txt");
FileInputStream fileStream = null;
try {
String temp = project + "/conf.txt";
temp = temp.substring(2);
fileStream = new FileInputStream(temp);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
newFile.create(fileStream, false, null);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// create closes the file stream, so no worries.
try {
myFile.writeTextFile(FILE_NAME, "Seconds", output);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
FileUtil is a class which only implements the methods write and read for the file.
The Exception I get when I try to open it begins with:
org.eclipse.core.internal.resources.ResourceException: Resource '/ProjectE1/conf.txt' is not local.
at org.eclipse.core.internal.resources.Resource.checkLocal(Resource.java:353)
at org.eclipse.core.internal.resources.File.getContentDescription(File.java:264)
at org.eclipse.core.internal.propertytester.FilePropertyTester.testContentType(FilePropertyTester.java:108)
I somehow have to get a relative path during the runtime. Because I am opening a new instance of eclipse in the program, where I can see the Project in the Project Explorer but can't open the conf.txt file because it is not local.
It looks like your resource is an absolute path to /ProjectE1/conf.txt, I'm confused why you are not using java.io.
This will help you understand relative paths, I think this may be where you are wanting to put your conf file.
File file = new File("conf.txt");
if(!file.createNewFile()){
//err
}
System.out.println(file.getAbsolutePath());
I had similar issue. This is how I fixed.
First created file in local file system (using java.io)
Did project refresh
Reload the file
File file = new File(project.getWorkspace().getRoot().getLocation() + project.getFullPath().toString() + "/relative_path_of_my_file");
file.createNewFile();
project.refreshLocal(IProject.DEPTH_INFINITE, null);
keywordFile = project.getFile("/relative_path_of_my_file");
I'm trying to generate an AST with help of the CDT Eclipse Framework from an C-File of a generated project in my workspace. But every time i try to get an TranslationUnit through
ICElement ice = CoreModel.getDefault().create(file);
ITranslationUnit tu= (ITranslationUnit) ice;
ice and tu are null and i get the following error
java.lang.NullPointerException
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.expandLocation(TeamPDOMImportOperation.java:135)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.getImportLocation(TeamPDOMImportOperation.java:126)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.run(TeamPDOMImportOperation.java:92)
at org.eclipse.cdt.internal.core.pdom.PDOMManager.createIndexer(PDOMManager.java:600)
at org.eclipse.cdt.internal.core.pdom.PDOMSetupJob.run(PDOMSetupJob.java:58)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
I have an Eclipse Plugin which implements an IApplication extension. In the run Method i have following code.
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("NewProject");
try {
if(project.exists()){
if(project.isOpen())
project.close(null);
project.delete(false, false, progressMonitor);
project.create(progressMonitor);
project.open(progressMonitor);
}
else{
project.create(progressMonitor);
project.open(progressMonitor);
}
if(!project.isNatureEnabled(org.eclipse.cdt.core.CProjectNature.C_NATURE_ID)){
CProjectNature.addCNature(project, null);
}
project.close(null);
project.open(null);
} catch (CoreException e1) {
e1.printStackTrace();
}
IFile file = project.getFile("stub.c");
try {
IPath workspacerawlocation = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
FileInputStream fileStream = new FileInputStream(workspacerawlocation.append("stub.h").toString());
if(!file.exists())
file.create(fileStream, false, null);
ICElement ice = CoreModel.getDefault().create(file);
if(ice == null)
System.out.println("ice is null");
ITranslationUnit tu= (ITranslationUnit) ice;
if(tu == null){
System.out.println("tu is null");
}
else{
IASTTranslationUnit ast = tu.getAST();
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Can anyone help? Did i miss some Plugin-dependencies or initializations?
Following up to my comment: in order to do some code manipulation, you have to have the CDT indexer working as it is required by C Model/AST. I suggest two possible ways of creating a C project configuration:
1] Look here for list of tests in org.eclipse.cdt.core.tests which create CDT projects. These projects contain very simple .cproject configuration, but some alterations to your needs might be required.
2] Save a .cproject from a working C project somewhere and when creating your projects, just copy it into. You may need to refresh the project somehow or close/reopen it so that CDT reloads the configuration.
As a example:
public class Hello {
public static void main(String[] args) {
try {
OutputStream os = new FileOutputStream(new File("c.txt"));
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Why was c.txt generated in the root path of current project other then the same path of the java file?
thanks.
Because the root of your project is your current working directory when starting the JVM.
You can check the user.dir system property to see what is your current working directory. If you access a file without a leading slash (Unix) or drive specifier/backslash (Windows), the files will be created relative to your current working directory.
You haven't provided a full path - this means that File constructor will use your process's current directory.
The path you provided will point to the project directory only if you want to change you can
also you can mention the full path where file is to be generated.
OutputStream os = new FileOutputStream(new File("c.txt"));
OutputStream os = new FileOutputStream(new File("D:\\c.txt"));