please, i've been struggling for days to get my applet to work, but i can't solve this
my goal is to write a file with the content of my html form
i'm able to reach the applet method, but it doesn't create the file
it's probably some issue with the certificate but i've tried several ways of using jarsigner found on tutorials, stackoverflow and videos, but none of them did the trick.
here is the process I'm following:
1 - create the applet
public class Rappan extends Applet{
public String formResult;
public String generateFile(){
java.io.File file = new java.io.File(System.getProperty("user.home")+"/jsonResponse.txt");
try {
file.createNewFile();
FileWriter f = new FileWriter(file);
f.write(formResult);
f.close();
return "succeess!";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error!";
}
}
}
2 - set my applet tag in html and my script to send the data:
function getForm(str){
appletRappan.formResult = str;
var x = appletRappan.generateFile();
alert(x);
}
$('form').submit(function(){
var str = JSON.stringify($(this).serializeArray());
getForm(str);
});
3 - compile the class, css folder and the html page into Rappan.jar file using:
jar -cvfm Rappan.jar conf.txt Rappan.class css index.html
conf.txt content is
Permissions: all-permissions
Codebase: *
Application-Name: Rappan
4 - generate a second and signed jar file using:
jarsigner -signedjar SignedRappan.jar Rappan.jar rappankeytool
5 - run html in browser, fill the form, choose to run the application when popped up..
nothing happens after that.
I tried some other ways, like having a different folder with the html and css in my desktop, then i would compile only the class, copy both class and jar files into this folder and run the html.. Same result as before
I also changed generateFile() to init() method and ran in eclipse. In this case, the file was created.
just for the record, the appelt is supposed to be used to collect data from different users in different computers offline through a form and generate a file for each one..
I appreciate any help.
Related
I have written a project where some images are used for the application's appearance and some text files will get created and deleted along the process. I only used the absolute path of all used files in order to see how the project would work, and now that it is finished I want to send it to someone else. so what I'm asking for is that how I can link those files to the project so that the other person doesn't have to set those absolute paths relative to their computer. something like, turning the final jar file with necessary files into a zip file and then that the person extracts the zip file and imports jar file, when runs it, the program work without any problems.
by the way, I add the images using ImageIcon class.
I'm using eclipse.
For files that you just want to read, such as images used in your app's icons:
Ship them the same way you ship your class files: In your jar or jmod file.
Use YourClassName.class.getResource or .getResourceAsStream to read these. They are not files, any APIs that need a File object can't work. Don't use those APIs (they are bad) - good APIs take a URI, URL, or InputStream, which works fine with this.
Example:
package com.foo;
public class MyMainApp {
public void example() {
Image image = new Image(MyMainApp.class.getResource("img/send.png");
}
public void example2() throws IOException {
try (var raw = MyMainApp.class.getResourceAsStream("/data/countries.txt")) {
BufferedReader in = new BufferedReader(
new InputStreamReader(raw, StandardCharsets.UTF_8));
for (String line = in.readLine(); line != null; line = in.readLine()) {
// do something with each country
}
}
}
}
This class file will end up in your jar as /com/foo/MyMainApp.class. That same jar file should also contain /com/foo/img/send.png and /data/countries.txt. (Note how starting the string argument you pass to getResource(AsStream) can start with a slash or not, which controls whether it's relative to the location of the class or to the root of the jar. Your choice as to what you find nicer).
For files that your app will create / update:
This shouldn't be anywhere near where your jar file is. That's late 80s/silly windows thinking. Applications are (or should be!) in places that you that that app cannot write to. In general the installation directory of an application is a read-only affair, and most certainly should not be containing a user's documents. These should be in the 'user home' or possibly in e.g. `My Documents'.
Example:
public void save() throws IOException {
Path p = Paths.get(System.getProperty("user.home"), "navids-app.save");
// save to that file.
}
I have a program I have written in Eclipse and it runs fine -- the HTML file opens when I run the program through Eclipse. But when I create a jar file of the program, everything else runs fine except this HTML file won't open in the browser (or anywhere):
operation.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
try {
File htmlFile = new File("help/operation.html");
Desktop.getDesktop().browse(htmlFile.toURI());
} catch (MalformedURLException MURLe) {
MURLe.printStackTrace();
} catch (IOException ioE) {
ioE.printStackTrace();
}
}
});
The rest of the program runs fine, and my images and sounds work fine and are opened, but this HTML file will not open in the menu or with the Ctrl+key shortcut. Your help is appreciated. Thanks.
When you have a file inside your jar, you cannot access it like you are doing now.
You need to read it as a stream, that's the only way.
Suppose your project is foo. Then help/operation.html will refer to
..\abc\help\operation.html
But the deployed jar file will not contain it.
You have include this operation.html file in your source code (where you write code).
Then eclipse (or any IDE) will add it into your jar file when you deploy it.
And now you can use your file as follows.
Suppose your file is present in as shown in figure.
Now you can refer your html file from any class. In this example referring it from
Accesser class.
File resFile = new File(Accesser.class.getResource("operation.html").toURI());
If you want to open your file in browser you will have to copy this file into the
user's System.
File htmlFile = new File("operation.html");
if(!htmlFile.exists) {
Files.copy(resFile.toPath(), htmlFile.toPath());
}
Desktop.getDesktop().browse(htmlFile.toURI());
Files is present in java.nio.file package
I migrated my project from Net beans 6.9.1 to Net Beans 7.3.1 and faced this annoying error a red exclamation icon on a random file jsp or java .
I opened them and did not find any error.
I tried some suggestions after searching Google to disable html and jsp validation with no luck , another suggestion was to delete the cache files under user directory folder cache at C:\Users\home\.netbeans\6.9\var\cache and also without luck !!!
resolve bug incomplete
sample of java file error
You can try to do the following ... it worked for me
rename the file of jsp or java to make the error go away for example
test.java renamed to test_.java and then renamed back to test.java
also same for jsp or xml
references
translate it to english
By working with netbenas on some projects in some of these projects
netbeans files mark some files with the symbol of admiration and the
message "Error parsing file". This occurs because of a problem
netbenas cache. The solution to this is to close the netbenas, clean
(delete cache files and start the netbenas will return. Here are the
different routes of some operating systems cache. WINDOWS: C: \ Users
\ AppData \ Local \ NetBeans \ Cache \ 7.2 \ MAC OS X: / Users //
Library/Caches/NetBeans/7.2 / UNIX: / home // .cache/netbeans/7.2
good luck
i fixed "Error Parsing File" in my Java file (IDE: Netbeans) by just deleting the space before the bottom most "}" and press enter. Basically, just do some modification in the file and save it again.
In my case I had a class similar to the following and Netbeans (8.2) showed no error inside the file, but in the file icon it showed a error of parsing the file:
public class FileUploadUtil {
private static interface WriteToFile {
public void run(File file) throws IOException;
}
private static interface UseFile {
public void run(File file) throws IOException;
}
private static void createAndUseTempFile(InputStream is, UseFile use) throws IOException {
createAndUseTempFile((file) -> {
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] bytes = new byte[1024];
int read;
while ((read = is.read(bytes)) != -1) {
fos.write(bytes, 0, read);
}
fos.flush();
}
}, use, "tmp");
}
private static void createAndUseTempFile(Image image, UseFile use, String extension) throws IOException {
createAndUseTempFile((file) -> image.writeToFile(file), use);
}
private static void createAndUseTempFile(WriteToFile write, UseFile use, String extension) throws IOException {
File file = null;
try {
String key = System.currentTimeMillis() + "-" + SecurityUtil.generateUUID();
String suffix = (extension != null) ? ("." + extension) : null;
file = File.createTempFile(key, suffix);
write.run(file);
use.run(file);
} finally {
if (file != null) {
file.delete();
}
}
}
}
The method:
private static void createAndUseTempFile(Image image, UseFile use, String extension) throws IOException {
createAndUseTempFile((file) -> image.writeToFile(file), use);
}
should be:
private static void createAndUseTempFile(Image image, UseFile use, String extension) throws IOException {
createAndUseTempFile((file) -> image.writeToFile(file), use, extension);
}
but Netbeans showed no errors inside the file, so I tried to reload the project, rename the file and so on.
Then I tried to compile with gradle and received the error:
FileUploadUtil.java:95: error: incompatible types: InputStream is not a functional interface
Then I realized that it was trying to call createAndUseTempFile(InputStream is, UseFile use) instead of createAndUseTempFile(WriteToFile write, UseFile use, String extension), but because the InputStream is not a functional interface and doesn't extends/implements an interface that has a method that receives a File, it couldn't call that method (and shouldn't!).
I think it's a Netbeans bug in this case, because it should show the error in that line.
Sometimes I had these issues with embedded JavaScripts in the JSP files, especially if the JavaScript parts contained JSTL EL expressions. In these cases the NetBeans project tree view showed a red exclamation mark ("Error parsing file") for the JSP file but when opening the file it didn't show a single error for a line.
Idea 1: Add HTML comments to the JavaScript part in order to make the JSP/HTML syntax highlighter engine ignore these parts:
<b>Very primitive example</b>
<script type="text/javascript">// <!--
var foo = ${myBean.bar}; // -->
</script>
Idea 2: Put as much JavaScript code as possible into external JS files. In general it's a good idea to avoid JavaScript code in JSP/HTML files as this allows you to use additional anti XSS measures like X-XSS-Protection.
May occur if having unnecessary lambda return statement in Netbeans 8.2
I'm not sure if this is helpful but I was using netBeans IDE 8.2 and one of my Dialogs exampleDialog.java showed a redmark on it and there were no errors in the file.
I was using Dimension wndSize;
wndSize = theKit.getScreenSize();
and in setting the window size I was using wndSize.getWidth(); and wndSize.getHeight(); these were wrong I changed them to wndSize.width; and wndSize.height;
and the red mark disappeared.
regards Michael.
No need to worry ! This is because after you make any changes in servets or jsp you need to save your file.
So first save your file then everything goes well.
This worked for me!
Having a issue with getDeskTop().open / .edit(file) in that it is working properly on development drive by opening the file but when I move the application (jar) to another drive I get no error and no response. The paths are hardcoded "/home/temp/" + file, the application creates folders on start, basically the application is a personal version system serializes file contents to XML, when selected it deserializes then writes the file to a temp folder then calls getDeskTop().open(file). The confusing part is that I also call getDeskTop().open(file) on the VersionControl.xml that the app creates and it works properly, checked the path vars to the file and they are correct. Here is the basic call, I get the path vars from a JTable cell:
case 2 :
File fr = new File((String) jt.getModel().getValueAt(tmpRow, 2));
javaxt.io.File ft = new javaxt.io.File((String)jt.getModel().getValueAt(tmpRow, 2));
//JOptionPane.showMessageDialog(null, fr.toString());
if (!AppVars.getIllegalExt().contains(ft.getExtension())) {
try {
Desktop.getDesktop().edit(fr);
} catch (IOException e1) {
e1.printStackTrace();
}
}
break;
It seems somehow I am missing a reference, the "Make" configuration is to extract dependencies into the jar.
I am currently working on an application, where users are given an option to browse and upload excel file, I am badly stuck to get the absolute path of the file being browsed. As location could be anything (Windows/Linux).
import org.apache.myfaces.custom.fileupload.UploadedFile;
-----
-----
private UploadedFile inpFile;
-----
getters and setters
public UploadedFile getInpFile() {
return inpFile;
}
#Override
public void setInpFile(final UploadedFile inpFile) {
this.inpFile = inpFile;
}
we are using jsf 2.0 for UI development and Tomahawk library for browse button.
Sample code for browse button
t:inputFileUpload id="file" value="#{sampleInterface.inpFile}"
valueChangeListener="#{sampleInterface.inpFile}" />
Sample code for upload button
<t:commandButton action="#{sampleInterface.readExcelFile}" id="upload" value="upload"></t:commandButton>
Logic here
Browse button -> user will select the file by browsing the location
Upload button -> on Clicking upload button, it will trigger a method readExcelFile in SampleInterface.
SampleInterface Implementation File
public void readExcelFile() throws IOException {
System.out.println("File name: " + inpFile.getName());
String prefix = FilenameUtils.getBaseName(inpFile.getName());
String suffix = FilenameUtils.getExtension(inpFile.getName());
...rest of the code
......
}
File name : abc.xls
prefix : abc
suffix: xls
Please help me in getting the full path ( as in c:.....) of the file being browsed, this absolute path would then be passed to excelapachepoi class where it will get parsed and contents would be displayed/stored in ArrayList.
Why do you need the absolute file path? What can you do with this information? Creating a File? Sorry no, that is absolutely not possible if the webserver runs at a physically different machine than the webbrowser. Think once again about it. Even more, a proper webbrowser doesn't send information about the absolute file path back.
You just need to create the File based on the uploaded file's content which the client has already sent.
String prefix = FilenameUtils.getBaseName(inpFile.getName());
String suffix = FilenameUtils.getExtension(inpFile.getName());
File file = File.createTempFile(prefix + "-", "." + suffix, "/path/to/uploads");
InputStream input = inpFile.getInputStream();
OutputStream output = new FileOutputStream(file);
try {
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
}
// Now you can use File.
See also:
How to get the file path from HTML input form in Firefox 3
I remember to have some problem with this in the past too. If I am not mistaken, I think you cannot get the full file path when uploading a file. I think the browser won't tell you it for security purposes.