Reference txt file from code in Intellij - scala

I have a very simple example in Scala for reading a text file. The example is made in IntelliJ. However when I try to use io.Source.fromFile("..\..\resources\example.txt"), I get a FileNotFoundException. The code works when I type in the absolute path.
Can anyone tell me how to use relative paths for specifying a file in Intellij project?
the directory structure:

Paths are relative to the current/working directory, i.e. the directory from which the java command is executed.
When an application is executed from IntelliJ, this working directory is configured in the Run configuration used to launch the application.
So, go to Run - Edit configurations..., then find your run configuration, edit it, and see what the field "Working directory" contains. By default, the working directory is the root directory of the project.
Note that, unless the String escaping rules are different in Scala from Java, the path must be separated by slashes:
"../../resources/example.txt"
, or the backslashes must be escaped:
"..\\..\\resources\\example.txt"
Also not that if your goal is to bundle the text file inside the jar of your application, you shouldn't use file IO to read it, but use the ClassLoader.getResourceAsStream() method.

Related

creating Executable Jar with input file on eclipse

I am trying to create an runnable Jar on eclipse.
After creation, Eclipse give me error message saying that its was created with compiler error.
When I ran it on the command line, it throw IOException, saying that I cannot find the input file.
I have file path within the project to read number of .jpg files.
I think these are not included for some reason when it creates the Jar file.
Can anyone help me with this?
Here is how I'm Accessing the files in my code
ImageIcon icon = new ImageIcon("src/images/" + (i + 1) + ".jpg");
file = new File("src/images/" + (i + 1) + ".jpg");
read = new Scanner(new File("intensity.txt"));
It depends on whether the images shall be packaged into the final jar file or loaded from some directory. The former will not rely on absolute paths as the latter one might, while the latter one allows easier adding of images or exchanging them -- but that really depends on what you need. (There may be many other caveats/advantages/disadvantages... you name it. Searching on stackoverflow with the right keywords may help you out, but read on)
Running a project from within eclipse sets up several things like classpath and the working directory is usually set to the project's base path. Hence, the executable may "incidentially" find the images. It may not always be that obvious what eclipse does. I suggest you have a look at and make yourself familiar with the "run configurations".
If you want to include the files into the jar-file, you may also have to adapt the project's Java Build Path >> Order and Export settings to include the image directory.
You may also want to have a look at the following Q/A: Load image from jar and outside it in eclipse

Where to put files I want to load with my program in SBT?

In SBT where do I put files I want to load into my program? I know I can use the /test/ directory, but the problem is I have no idea in what context sbt is executing my scala program. From where does it execute so I know how to write a directory string to grab it?
Place them under src/main/resources or src/test/resources as described in Directory structure

How exlude properties when building executable jar in Eclipse?

This question has been covered here before, but the only solutions I could find were in relation to a project using Ant or Maven. I am using neither. Here is the situation:
I have some application parameters in a properties file. This file is located in my Eclipse project (but in the src folder) and used when I run the application from Eclipse. In addition, I would like the application to also run as an executable jar file, in which case the user can provide the name of a properties file to use in a command line parameters.
The problem now is that the properties file from the project is always packaged into the executable jar and therefore the user is not able to easily modify the properties (yes, I know that (s)he could unzip the jar, but I want to avoid the extra steps).
How can I prevent the properties from being packages into the executable jar file?
Cheers,
Martin
Create a executable jar without properties file in it. Place both jar and properties file in a folder. Now add little code in your main program which should look for a properties file in the same folder and get the complete path of it. And then you can do something like this
System.getProperties().load(new FileInputStream(completepath));
So now your properties will be loaded into system properties with out affecting the actual system properties. You can access your properties by System.getProperty("Propertyname");
Hope this helps. Let me know if you have more questions.

Set a classpath just for jUnit in Netbeans [duplicate]

We have an application developed in NetBeans, based on the NetBeans platform. There's a 3'rd party program that we have a runtime dependency on, more specifically a jar in the other progams lib folder.
How should we include the other progam's jar in our classpath?
The recommendation from the other progam's manufacturer is to set environment variable CLASSPATH to include
C:\Progam Files\Other Program\lib\theJAR.jar
And if that's not possible, we should copy theJAR.jar to JRE-HOME\lib\ext
We must not copy theJAR.jar anywhere else, that might cause it to stop working...
Now NetBeans takes no notice of what's on environment variable CLASSPATH. Their recommended way seems to be to make a wrapper, but that would lead to copying the jar, unless there's some way to make a wrapper module that points to CLASSPATH?
At the moment we are copying the jar into JRE-HOME\lib\ext. But then there's unnecessary hassle when we install some java update.
Do you have any solution to this problem? It seems like something that might be simple, but I haven't found the right place to look yet...
Edit: The application is ant-based.
From the documentation for the Module System API's overview of the runtime infrastructure (bottom of the page under the section "Common Problems and Solutions"):
Q: Can my module add a library JAR to the classpath from outside the
IDE [read: platform] installation?...
A: Not easily. You have a few options:
Add an entry to ide.cfg [your app's .config file]. For example:
-cp:a c:\eak\lib\eak.jar This startup file provides the ability to add classpath entries to the IDE's Java invocation.
...
It goes on to list two more options. The second option is the same solution you've come up with. The third is to "partition your module and use a new classloader" which I can't recommend either way since I have no experience doing this (but it's worth a read).
Assuming that this first option is what you are looking for, you will need to add a custom .conf file to your project and point to it in your project.properties file, like so: app.conf=nbproject/my.conf. This will add your custom .conf file to your app's install directory instead of the default config file that is normally added. Of course, you'll need to add the -cp:a c:\eak\lib\eak.jar entry to this custom config file in order to load the .jar.
During development you'll need to add the following entry to the project.properties file: run.args.extra=-cp:a c:\eak\lib\eak.jar. This will pass the command line option to your debug instance.
You can add that .jar file by following the steps below:
In the left side panel named "Projects" open your project and right click on the "Libraries", then select "Add JAR/Folder...".
Then select your .jar file from the location where you have stored it in the next dialog box that opens and then press "Open".
Vola Finished!!! (with the same process you can add other libraries also like JavaCV, JMF,etc)
And Now You Can Use That .Jar File From Your Project Library.
Hope It Helps.

XML parsing in scala

I am parsing XML file in scala using
val data = XML.loadFile("changes.xml")
I have the changes.xml under the src folder. When I run this the code throws a FileNotFoundException. Any idea how to solve it or any insight on how scala looks for the
files in the classpath would be helpful.
See what the current directory is using
new java.io.File(".").getCanonicalPath()
Since you're opening the file with a relative path, it looks for the file in the process's working directory.
Since you're placing your file in the source tree, I assume you want to ship it with the application jar.
Then tell your IDE to copy the file to the ouput folder (maybe this is already happening) so it lies in the classpath. If you place your file in the same folder as the class from which you want to load you can simply do the following. Use in Java/Scala
Class.getResourceAsStream("changes.xml")
Link to API doc.
Edit
You can use XML.load("changes.xml") and I think it will load the file in the same way as Class.getRessourceAsStream. So try putting your xml file into the same folder as the class and make sure the build process copies it into your binary output folder.