Include file as input stream in scala - scala

I'm having problems with using a file in my code. I'm working with javaPNS and trying to get it to work. What I think is wrong now is how I get my file for the keystore parameter in the Push.alert method in javaPNS.
What I'm really wondering is how do I "get a resource" properly so it has the correct format?
Here's the exception that is thrown:
javapns.communication.exceptions.InvalidKeystoreReferenceException:
Invalid keystore parameter (null). Must be InputStream, File, String (as a file path),
or byte[].
at........
And here's how I currently include the file:
val keystoreFile = getClass.getResourceAsStream("/app/conf/cert.p12")
If we look at the exception I get, do you think this above is the problem? How should I include the file properly in Scala?
regards,

The problem you are seeing has to do with the fact that the InputStream you are loading is coming back as null. This happens when the file you are trying to load can not be found on the class path. Try changing your code to:
getClass.getClassLoader.getResourceAsStream("app/conf/cert.p12")
You will notice I removed the leading slash on the file path as I believe this is your issue. Then just make sure that whatever the parent directory to "app" is is on your class path.

Related

Nothing happens when using I.attachFile to upload a document , can you suggest what I should do

Is a drag-and-drop field required for the I.attachFile('element', 'filePath') to work? Because currently nothing happens when I try using the attachFile method. No error message or any issues displayed even when using --verbose.
This is the element where I try to attach my file //input[#type='file']. I also verified that I have the correct fileName and filePath since I tried using a wrong file name and it returned an error.
I am currently using:
codeceptjs: ^3.3.6,
playwright: ^1.19.0
I tried changing the file I'm about to upload to see if my file is the problem, but when I do upload the file manually in the page it works as expected; but when trying to do it in my code, nothing happens.
Expected: The uploaded file's name should be displayed
Actual: Nothing happens; no file name displayed; no error message returned in the logs
i expected the file to be uploaded.

How to use custom language highlight syntax in Gtk SourceView?

I'm trying to create my own language definition, and use it for highlighting the syntax in my app.
The issue I have is that, when trying to access the language definition from my app's data folder (/usr/share/myapp/), even using the c.lang file copied from /usr/share/gtksourceview-3.0/language-specs/, just to test, I get this error at runtime:
GtkSourceView-WARNING **: Failed to load '/usr/share/myapp/c.lang': could not find the RelaxNG schema file
So it's asking for some schema file? So I went forward and copied every file from the language-specs folder that isn't a lang file, which includes: language.dtd, language.rng and language2.rng.
Now, when I run again, I get these errors:
GtkSourceView-WARNING **: in file /usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang: style 'def:comment' not defined
GtkSourceView-WARNING **: Failed to load '/usr/share/com.github.aleksandar-stefanovic.urmsimulator/c.lang': unable to resolve language 'def'
What does that even mean? Is that something namespace-related? It is very peculiar, because the exact same file is working perfectly when in gtksourceview folder.
Any clues? Do these "RelaxNG" files work only in their original directories? If so, how can I change that? I've looked into the files, but failed to find any reference to their original folder...
This is the source-code (in Vala) related to the issue:
var manager = Gtk.SourceLanguageManager.get_default ();
string search_paths[] = {"/usr/share/myapp", null};
manager.set_search_path (search_paths);
var buffer = new Gtk.SourceBuffer.with_language (manager.get_language ("c"));
The application does in fact find the language "c", I've checked by debugging.
You have to preserve the Gtk.SourceLanguageManager's original search path when you add your own. Append to it instead of replacing it.

TagLib# Exception on File.Create

I am trying to use Taglib# to add id3v2 tags to a file.
I have it working on some files - however, on certain files it will fail. (Possibly due to file corruption).
if (ext == ".aiff" || ext==".aif"){
//force reading of aiff files
file = TagLib.File.Create(filename, "audio/aiff",TagLib.ReadStyle.None);
}
The Create() call throws this exception:
Provided data does not start with the file identifier
Based on the documentation, I'm not sure how I can fix this (or add a file identifier).
Not sure there was a way to do this. The solution was to run the files through FFMPEG and create new AIFF so that they would produce PROPER AIFF files. Then TagLib# works properly.
Based on my own experiments, TagLib Sharp also throws exception on the Create() method "if" corresponding file has no ID3 tag at all. I just tested with some files, removed tags using TagLib Sharp, then tried the Create() method on the same file; same exception happened.

Allow for non-existent files in FileFieldEditor [duplicate]

I'm setting up a series of preferences in my Eclipse (3.5.2) application and I'm having a problem with the FileFieldEditor. I want to allow the user to specify a log file to print output to. Often, this will be a new file. But when I use the file select dialog with FileFieldEditor, it complains that the file doesn't exists ("Value must be an existing file"). Is there a way, without extending the FileFieldEditor class, to suppress this error and have Java create that file if it doesn't exist? Thanks!
When I look the source code of org.eclipse.jface.preference.FileFieldEditor, the only solution would be to extend it and write your own version of a FileFieldEditor, with:
an overwritten changePressed() method in order to keep the file path even if the file does not exists
an overwritten checkState() method in order to avoid that error message.
So I do not see a way to avoid that FileFieldEditor extension here.

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.