I am writing an Eclipse plugin where I want to read a file within the project and do something on it.
For example the file is located under : Project testplugin and path : com/flow/FlowMain.java
I want to programmatically read this file and add some code in it.
What I trying is :
String base = Platform.getBundle(config.getPluginId()).getEntry("/").toString();
String relativeUri = "com/flow/FlowMain.java";
File f = new File(base+relativeUri);
This obviously fails because the value of "base+relativeUri" returns :
entry://1079.fwk5184781/com/flow/FlowMain.java
So how do I go about getting the complete file path from within the plugin ?
'entry' is a protocol defined by equinox, so you can get the real path using org.eclipse.core.runtime.FileLocator.toFileURL(URL).
Use the below code to get the plugin path :
String workSpaceRootpath=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()+Relativepath of a file;
Related
At the moment i build a chat similar in wahtsapp.
I would like upload some local files (pdf, txt, image...).
Like this:
I try to get the local files via path_provider.
But i dont get access to download folder.
My problem:
I don't know how i can access the dowload folder with path_provider.
See my code:
//load local files from download folder
_loadLocalFile() async {
//absoulute path to download file
var file = io.Directory('/storage/emulated/0/Download/')
.listSync(); //use your folder name insted of resume.
//console output -> []
//other try to get download folder
List<io.Directory>? documents = await getExternalStorageDirectories();
//console output -> [Directory: '/storage/emulated/0/Android/data/de.securebuy.securebuy/files', Directory: '/storage/1BEC-0908/Android/data/de.securebuy.securebuy/files']
print(documents.toString());
}
I also add to AndroidManifest following rules:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGEā/>
Anyone have a idea how i can build a function similar in image?
Is that possible?
Many thx
till now , pathprovider isn't going to let you in downloads file , so if you are on android you can just write the downloads directory , else using android_path_provider it is a null-safety package and you can accsess it via :var downloadsPath = await AndroidPathProvider.downloadsPath; , on ios you can get only the app directory cuase ios makes it's own file for every app thus we use 'pathprovider' with Directory appDocDir = await getApplicationDocumentsDirectory();
Update :
i see what you are trying to achieve and this is not what pathprovider pcakage is used for , you have to use file_picker , read thier docs , i thinks that this is what you need
This is the code that I have for now but the output from this is: /Users/zakariasfridriksson/runtime-EclipseApplication/is.ru.cs.PapyrusActivityLogger.example (runtime-EclipseApplication this is wrong like it is missing some part of the path)
Code:
IProject project = root.getProject("is.ru.cs.PapyrusActivityLogger.example");
System.out.println(project.getFullPath());
IFile csv = project.getFile("logger.csv");
String finaldestination = root.getLocation().toString() + project.getFullPath();
System.out.println(finaldestination);
You can just call getLocation on the IFile:
IPath csvPath = csv.getLocation();
String dest = csvPath.toOSString();
Note: runtime-EclipseApplication is the test workspace that Eclipse has created for you when you run plug-ins using 'Run as Eclipse Application'. So your original code is also giving the correct result.
So I want read a image file in scala worksheet but I don't want use absolute path so others no need to change the code before run, how can I achieve that? the following method return the IDE path, not my project path.
the image file and the worksheet file are in same directory
val path = File(".").toAbsolute //D:\app\idea2016\bin\
System.getProperty("user.dir") //D:\app\idea2016\bin\
I'm currently developing a scala library and wanted to get a file that is inside it, event when compiled as a Jar dependency. The problem is that when executed from another project where the library is imported, the path is relative to that project. Here is the code to get the file :
private val pathToDocker: Path = Paths.get("src", "main", "resources", "docker-compose")
What can I do to look for my file inside the imported dependency ?
The file won't be in a file system -- it'll be in the compiled JAR.
You can get a JAR resource using a class loader. Here's an example of how to do that in another codebase.
Utility function:
https://github.com/hail-is/hail/blob/6db198ae06/hail/src/main/scala/is/hail/utils/package.scala#L468
Usage to load version info:
https://github.com/hail-is/hail/blob/6db198ae06/hail/src/main/scala/is/hail/package.scala#L21
There is a JarUtil - from an answer of access-file-in-jar-file translate to Scala:
import java.util.jar.JarFile
val jar = new JarFile("path_to_jar/shapeless_2.12-2.3.3.jar")
val entry = jar.getEntry("shapeless/Annotation.class")
val inStream = jar.getInputStream(entry)
As you mentioned scala.io.Source.fromResource works only within your project:
Source.fromResource("tstdir/source.txt")
Make sure the file is in the resources directory, like:
The way I found to achieve getting the path inside a jar depedency was creating a singleton (scala object) that has a method that loads the files. Since it is inside the Jar, the resulting path is relative to the jar itself. Here is the code :
object ClassLoaderTest {
def dockerFile: File =
new File(getClass.getClassLoader.getResource("docker/docker-compose.yml").getPath)
}
When called from a Trait (or an interface), the resulting path is :
jar:file:/home/myname/.ivy2/cache/com.mypackage/libraryname/jars/libraryname.libraryversion.jar!/docker/docker-compose.yml
I know there have already been some questions about relative paths, but I Keep failing to get JavaFX FXML loader to load a resource from a package other than itself.
The loading class is located in the package gui.controllers and the fxml file BarSheet.fxml is located in the package gui.resources.
What should i now put in :
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("src/gui/resources/BarSheet.fxml"));
Thanks in advance
SOLVED: The fault was in the fact that my fxml file contained an error so i didnt know when i entered the right path because it would still not work...
The path should start with / to indicate the path starting from the root followed by packages/filename. So in my case
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/gui/resources/BarSheet.fxml"));
As a complement when you have a package like com.company.view, and inside you have the .fxml file in order to make this work you have to put the line like this:
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/com/company/view/file.fxml"));