Write log from applet, while debugging in eclipse - java

I am trying to write a log file from an applet.
When running as a Java application, I am able to write to the files,
but when running as an applet, I get .\logs\test.log (The system cannot find the path specified).
How do I permit it to write to disk, while debugging using eclipse?
EDIT: is it because of the backslashes?

You should write whatever you want from applet in temp file, to create temp file Try this
. Also you get system temp folder in java and create your file there, Read this. #Yoni is right you have limited permissions when you are in applet.

Related

can I use .txt files in JAVA using VS code?

I am trying to access the text file in java and the code I have works in other IDEs but in VS code it keeps saying File Not Found - (produced from my exception handling)
I can't find anything about how to access text files with VScode. Is this possible please and does anyone know how?
I have tried to create the file in VScode with the new text file option and also by loading a pre-existing file and get the same result each time
Place the .txt file in the project root directory so that the code reads to the file location. As shown below
If the file is not with the project, use absolute paths in the code.

How To Open Files With .Jar (Java) Application?

Tough question here. How do you open a file in java, in the way that when you double click on the file it automatically opens in a java application.
I'm making a musicplayer (first real big Java project for me) and I have no clue how to acheive this. When you open, lets say, a .mp3 file it will open in whatever default program you have selected for it (such as VLC mediaplayer or Windows Media Player). What I want is to be able to set the .jar file of my application as the default program for .mp3 files, and then to be able to actually launch the files in the application.
When I currently try to open a file with the application I get a windows error saying "This app cannot be executed on your pc". But when I launch the .jar itself without doing it by trying to open a .mp3 file it runs just fine.
Does anyone know how to acheive what I want? Many thanks in advance!
---edit---
I do not mean that you can select a default program for the mp3 file. The problem is that windows throws the error shown above, and that I dont know how to handle the application being launched by opening a file (which does not ope due to the error).
I think the problem is that you have to open a file with a .exe , so you sshould use an exe wrapper (I use jsmooth: download here)
BUT, before you do that, you need to accept that info. So in the main class, the "args" is a list of info about how it's being launched. If you are opening a file, the array's first argument will be the opened file's destination. SO I would accept it like this:
if (args.length > 0) {
File f = new File(args[0]);
start_the_application_with_a_file(f);
} else {
start_the_application_without_a_file();
}
C:\ProgramData\Oracle\Java\javapath\java.exe -jar "C:\Program Files\YourApp.jar" %* within a batch-file (.cmd) might do it.

How can I programmatically find the path of JNLP file? I am using Java Web Start to generate the JNLP file

How can I programmatically find the path of JNLP file?I am using Java Web Start to generate the JNLP file.
I know that manually you can find the JNLP file in the Java Cache Viewer in Resources with the name launch.jnlp, but I really need to know if there exists a Java class that can programmatically find the jnlp file by searching the in-memory cache.
How can I programatically find the path of JNLP file?
It is hidden deliberately. If you can find it from within code (shorting of asking the user to browse to it), the JRE has a security bug.
..for launching the installer I need the application jar files path
No you don't.
Put the installer inside a Jar.
Add the Jar to the resources of the app.
Get an URL using getResource(String).
Read the byte[] and write it to a temporary file at a known location (e.g. java.io.tmpdir).
Launch the installer from the known location.
String jnlpPath = System.getProperty("jnlpx.origFilenameArg");

How to write a Java program to open a specific file extension (.pef)?

I need to write a java program to open a specific file extension (This is to open a .pef file). This java program is being written to open this .pef file and print it. But I don't know how to write a program that can open the .pef file extension by default.
See:
Desktop.open(File) Launches the associated application to open the file.
Desktop.edit(File) Launches the associated editor application and opens a file for editing.
Desktop.print(File) Prints a file with the native desktop printing facility, using the associated application's print command.
This presumes the application has already been associated with the file-type. To create the file association, launch your app. using Java Web Start & declare the file type in the launch file.
This solution is Windows specific
In this post I am assuming that your program is already capable of reading files of type .pef and printing them. If not then please refer to Treebranch's and Himanshu's posts here to figure out how to do so. Next I will assume that your program is to be invoked on the Windows command line as:
program.exe <filename.pef>
To programatically change the association, follow these steps:
Define a file type
ftype peffile="program.exe" "%1"
Remember to use the absolute path of program.exe here.
Associate this file type with .pef
assoc .pef=peffile
I am not familiar with this file type, but I am guessing you are going to have to read the bytes of the file and then go from there. You should be able to use a FileInputStream. Try looking at this tutorial.
You can use FileInputStream to read a file.
FileInputStream fr=new FileInputStream("xyz.pef");
int i=0;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();

Where does Netbeans read files from?

I'm trying to read from a text file in Netbeans. In the top level of my project directory I have foo.txt. Then in my code I have:
File file = new File("foo.txt");
It throws a FileNotFoundException, however. It's a Java web application using Spring and Tomcat, but I'm not sure if those details matter since I'm running the whole thing inside Netbeans. Basically, I just want to know where I need to put the file so Netbeans will read it.
Update - good call guys, it's looking in Tomcat's bin directory. Now this may be a stupid question but, how would I go about getting it to look in my top level project directory? I feel like dropping text files into tomcat's bin would be innapropriate.
You can try printing the absolute path of the File object to see where it is looking on the filesystem.
System.out.println(file.getAbsolutePath());
I would use the following to figure out where to put the file:
System.out.println(System.getProperty("user.dir"));
To directly answer your question, If you're running an application on Tomcat, files will be opened from the current working directory. That will likely be the bin/ folder in your tomcat directory.
You can find out for sure where your program is looking by examining the result of file.getAbsolutePath().
However, for web applications, I would suggest putting files you need to read in your classpath so you don't have to depend on a certain file structure when you deploy your web application.
try System.getProperty("user.dir") to get current working directory

Categories