How to save code for Java applets - java

I am thoroughly confused as to what part of my code I need to put onto a website in order for me to embed this as a gadget in Google Sites. One source that I found said that I should have both my main class as one file and my whole project as another. I am using Google Code, and have gotten my main class (which is saved as a .java file), and the rest of my project (which is saved as a .jar file) onto the code file cabinet.
Currently, I am getting an error with my code not being able to find the class that contains my main(String[] args) {} method. Officially, it is a ClassNotFoundException, with the text Main.java, which is the name of my 'code' in HTML. If you want to see it for a reference, here is the HTML that I am using for my Google site:
<applet name="UnderLD" code="Main.java" archive="UnderLudumDare.jar"
codebase="http://wierd0games.googlecode.com/svn/"
width = 300 height = 168>
Sorry, this Applet could not be started. Please make sure that Java 1.4.2 (or later) is installed and active in your browser (Click here to install Java now)
</applet>
However when I paste the link for the codebase in my brower, the java.Main class (the one with my main method) is the first on the list. To me, I am doing everything correctly according to all of the tutorials I have found, so what am I doing wrong?

code should be the name of class file for the applet, not the name of the source file. The trailing .class is optional but Sun now recommends omitting it. All of the main browsers will work with or without the .class suffix.
So both of the following should work, provided Main.class is in the root of the jar file:
code="Main.class"
code="Main"
Source Using applet, object and embed Tags
code = appletFile
This required attribute gives the name of the file that contains the
applet's compiled Applet subclass. This file is relative to the base
URL of the applet. It cannot be absolute. One of code or object must
be present. The value appletFile can be of the form classname.class or
of the form packagename.classname.class.

Related

System.getProperty("user.dir") with Eclipse IDE

I've been blasting through the Java Swing tutorials on zetcode.com
I've made it all the way to Basic Swing Components II, the JTextPane component.
In this example, an HTML document is loaded into memory and placed into the textpane.
In order to find the file, zetcode.com uses:
String cd = System.getProperty("user.dir") + "/";
textPane.setPage("File:///" + cd + "test.html");
My IDE of choice is Eclipse Kepler.
I have written the code for this example's class and created the HTML document exactly as zetcode.com has shown on the page. I have placed the HTML file in the same source folder and package as the class which uses it.
But when I run the code, I hear a Windows system error sound and the JFrame pops up without any text inside the textpane.
EDIT 01:
I've named the package "com/zetcode/swingtutorial/basiccomponents/".
I've tried using getClass.getResource("/com/zetcode/swingtutorial/basiccomponents/test.html")
and I figure I must have typed this correctly because I do not get an IOException.
EDIT 02:
Here's another interesting thing:
In zetcode's system, they've used "File:///", which caused Windows to play an error sound.
But when I tried "File://", no error sound plays. D'you think that was just a typo on their part?
Either way, my html doc still isn't displayed on the pane. :S
Do you know what I could be doing wrong?
Many thanks for your help!
Try this:
textPane.setPage(YourClass.class.getResource("test.html"));
If its in package
textPane.setPage(YourClass.class.getResource("/packagename/test.html"));
System.getProperty("user.dir");
Gives you the root context location or the project location folder or you can say the current directory when JVM was started
Accordingly give the path or alternately
Try loading this way
String pathOfHtmlFile = Thread.currentThread().getContextClassLoader()
.getResource("yourHtmlFile").getPath();
textPane.setPage(pathOfHtmlFile);
provided the file is in the classpath.
System.getProperty("user.dir")
Returns the "working folder" from which the application was launched. In Eclipse I believe the working directory is the top level project folder (not in src) or it's in the folder that contains the compiled .class files. Try copying the file to those folders and see when it works.

Error called"NoClassDEFFoundError "comes when running a applet

Hey guys i have a problem that has been wasting my times for hours which is that i get an error saying "NoClassDEFFoundError Wrong name(bigFish/BigFish)" when i try to run my html file which include a class and that contains a applet. bigFish is my project name and package name. BigFish is my class name of the class which contains the applet. and i have located my html file called BigFish in where the BigFish class file is located. how to do this. this comes in firefox. and i can't shift to chrome. its not working since month a back(doesn't even open). how can i see my applet run on web?
..i have located my html file called BigFish in where the BigFish class file is located.
That probably won't work. A loose class file in the bigFish package needs to be in a similarly named directory of the server. So, assuming the HTML is in directory www on the server, the structure should look something like this:
www directory
bigFish directory
BigFish.class
BigFish.html

Embedding multi-class applet with .jar

I have a multi-class applet that has been exported using Eclipse as a .jar file. The jar file is called chess.jar, and the class I compiled and ran from in Eclipse is called Chess.java, and the binaries are Chess.class. The following code is the HTML I am trying to use to embed this applet into my website.
<APPLET CODE=Chess.class
ARCHIVE="chess.jar"
WIDTH=700
HEIGHT=700
CODEBASE="mywebsitewherethefilesarebeinghosted.com/"
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason.">
Your browser is ignoring the <APPLET> tag!
</APPLET>
I get the error ClassNotFoundException Chess.class
I have done applets before that have only one class successfully here , and I am using almost exactly the same HTML except for the CODEBASE and I have added the ARCHIVE tag.
As an applet, it has no main class. I am not very familiar with the MANIFEST.MF file, and I'm not sure if I need to utilize it for this purpose. My Chess.class calls some other classes like Pawn.class and Knight.class and they are all in chess.jar. Any help would be greatly appreciated.
EDIT
I created a local HTML file that runs the .jar locally and it runs perfectly.
<html>
<applet code=Chess.class
archive="chess.jar"
width=700 height=700>
</applet>
</html>
I think it is a simple matter of the contents of your HTML. I see you have a second start tag that I think would be confusing the browser. The browser would think the first one does not have an end tag and that may be the reason it is being ignored. Remove that and try again.
Even if this isn't the problem it's bad practice to leave tags open like that. Your error message should also be a bit more meaningful for poor users who don't know what an applet tag is.
It seems that CODEBASE follows / as directory separator unlike . in case of CODE attribute
so try replacing your CODEBASE value with proper directory structure separated by /
check example here and here also

Applet on Website seems to hate Packages

So I made an Applet (not JApplet) and proceeded to upload it to my website. I put all of my .class files into a package, exported the project from eclipse to a .jar, and uploaded that to the public_html folder of my website.
In my HTML code, I put
<applet ARCHIVE="BallShooter.jar" CODE="BallShooter" width=500 height=500> </applet>
However, it seems that I kept on getting the error "ClassNotFoundException"
If I reupload the .jar WITHOUT the package, it worked fine. Could somebody please explain to me how to fix this?
For those who are wondering, this is the structure of the things in the website
/public_html/myAppletJar/myPackage/a.class
/public_html/myAppletJar/myPackage/b.class
/public_html/myAppletJar/a.png
/public_html/myAppletJar/b.png
Check all these points:
if the applet class is BallShooter, and is in the package kikiotsuka, then its source code must start with the line package kikiotsuka;. The full name of the class is thus kikiotsuka.BallShooter.
in the jar file, you should thus have a directory named kikiotsuka at the root, and this directory must contain a file named BallShooter.class
since the name of the class is kikiotsuka.BallShooter, that's what the code attribute of the applet HTML element must contain: code="kikiotsuka.BallShooter" width=...

Opening input file using java code in JSP

I am using JSP on NetBeans.
In the java code, I am trying to read data from a file. First I open the file by specifying its path.
Because my code runs on a server (GlassFish), I would like to have my file path independent of the machine where it runs. Therefore, I want to start the path with the folder name that contains the file which is saved on the root of the project directory. I tried so hard to achieve that but I couldn't.
I read online and I found this way, but it still doesn't work:
<%
//building the tree here.
GraphBuilder tree = new GraphBuilder("${pageContext.request.contextPath}\\src\\java\\Database\\OptimizedFullTermFile.pad");
%>
Can anyone help? Thank you.
Just read it from the classpath. Given that you're using the typical src folder representing the Java source code (the Java package/class structure), I assume that the file OptimizedFullTermFile.pad is placed in the Java package java.Database (eek, a capital in package name? lowercase it all). In that case, it's already in the classpath and thus you can just get it straight from there as follows:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("java/Database/OptimizedFullTermFile.pad");
// ...
As to your failed attempt: the EL ${pageContext.request.contextPath} isn't ever going to work in a scriptlet. Even if it did, it's not the right thing, it returns the context path in the webapp URL which is absolutely not part of the local disk file system path, let alone the classpath. Using scriptlets is strongly discouraged since a decade, by the way.
See also:
getResourceAsStream() vs FileInputStream
Where to place and how to read configuration resource files in servlet based application?
You can try the following code to get your directory file in project from the following code.
this.getServletContext().getRealPath("")+[your file path in project web directory]

Categories