java.lang.ClassNotFoundException when trying to load applet from a .jar file - java

Sorry for this question but I'm a newbie to Java. I tried searching for similar problems, and I did find some answers, but none worked in my case, so here it goes:
I'm trying to load a java applet on a web page. The applet is supposed to be loaded from a .jar file (UCFApplet.jar), but I cannot get it to load no matter what.
Here's the html for the applet:
<applet id="UCFApplet" height="200" width="200"
code="com.fujitsu.test.applet.UCFApplet"
archive="UCFApplet.jar">
</applet>
The jar file is on the server, on the same folder than the html file that is trying to load it. Inside the jar file there's a UCFApplet.class file on com/fujitsu/test/applet folder.
The error I'm getting on the Java console is the following:
Java Plug-in 1.6.0_29
Using JRE version 1.6.0_29-b11 Java HotSpot(TM) Client VM
User home directory = C:\Users\PTMAIAJ
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
load: class com.fujitsu.test.applet.UCFApplet not found.
java.lang.ClassNotFoundException: com.fujitsu.test.applet.UCFApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: com.fujitsu.test.applet.UCFApplet
As you can see, I'm using jre 1.6.0_29 build 11, which, from what I get on the Oracle page, is the latest build. This happens on all browsers (IE 9, FF, Chrome).
I suspect it may have something to do with not being able to access the .jar file because if I change the html code to a dummy name such as archive="thisFileDoesNotExist.jar" it gives me the same error. But I don't really know what to put there in order to make it work.
Any idea about what I can be doing wrong ?
Thank you and best regards,
Joao Maia

Your descriptor seems to be correct, so try the following.
First check that browser can download the jar. Type the direct URL to jar file into your browser. For example if your html can be accessed by typing
http://localhost/myapp/mypage.html
type
http://localhost/myapp/UCFApplet.jar
If you can download the jar and it is identical to your jar continue.
Check the jar itself. Type in command line
jar vft UCFApplet.jar
and see output. You should be able to find your class:
/com/fujitsu/test/applet/UCFApplet.class
BTW do you know that applet tag is deprecated and is not supported by Opera and Chrome? To support all browsers you should use tag object

Try turning on tracing in the java control panel. It will then produce a log file in the following path that may help:
%USERPROFILE%\AppData\LocalLow\sun\java\Deployment\log
In my case I had an error which only appeared when I turned on tracing, and which presented as simply a "ClassNotFoundException":
java.util.zip.ZipException: duplicate entry: META-INF/LICENSE.txt
The applet loader was rejecting the jar because the zip file had a duplicate file entry. Strangely appletviewer.exe was able to load and run this jar fine.

For those of you running behind a corporate proxy. According Oracle ClassNotFoundException is "a common error that is seen if the proxy settings are not correct".
See: How do I configure proxy settings for Java?

Related

How to get around the (Access is denied) to a folder that your java program is saving a file to?

I was wondering if someone could help me find a good resource to help me fix this issue. I'm trying to create/save a batch file in the startups folder. Here's the general path: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\startup_PMA_popup_check.bat
I've read a lot of things saying that I should simply elevate the privileges of my java program.
I read that if I was to make my jar file a windows executable, then the rights of the program are automatically elevated to admin privileges.
What you're seeing below is my jar turned into an exe and run. The error I got was the same error as the one I got from running my jar. I no longer think that the problem is simply giving my program admin privilege.
I performed a test outside of my program by going to this path and attempting to move a file into the folder. Assuming I'd have no issue "since I am root already", I went ahead expecting to get no prompts. What I got was a popup saying that I needed to confirm that I was Admin & to confirm that I wanted to put the batch file in the folder.
My question is how should I make my java program issue this prompt to the user or something so my program can continue & not crash like shown below??:
Exception in thread "main" java.io.FileNotFoundException:
C:\ProgramData\Microsoft\Windows\Start
Menu\Programs\StartUp\startup_PMA_popup_check.bat (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at java.io.PrintWriter.(Unknown Source)
at java.io.PrintWriter.(Unknown Source)
at prioritymanagementassistant.Background.createBatchFile(Background.java:106)
at prioritymanagementassistant.Main.main(Main.java:930)
Thank You for your time,

Lauch Eclipse SWT window application from command line error

First off. I am a newbie and green with respect to Java programming.
I created a Java Eclipse "SWT" "Application Window" on a Eclipse Kepler 4.3.1 (32-bit) running WindowBuilder and SWT (swt.jar). The design view is a composite.
The java application runs fine from Eclipse. I simply press the play button and presto.
Problem:
Issuing
java -cp . PkgNetAccelerator32.netAcceleratorApp
yields the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Composite
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
I was doing some research and several posts on other issues suggested that the cure is to create a manifest. I saw an article that said something like:
Select the src folder, right click and select export, then Java, then JAR file, specify Manifest.jar and a path on the folder, make sure the "Generate the manifest file" is selected, and press finish.
I created the manifest file and tried launching it with the following command line:
java -jar ../Manifest.jar -cp . PkgNetAccelerator32.netAcceleratorApp
That yielded the same error message above.
I saw another article that said that one should add in the swt.jar file, but that is taken care of by the manifest. No? Specifying "swt.jar" instead of the manifest that I created resulted in an error message:
no main manifest attribute, in ../swt.jar
That error message makes sense.
I am at a loss. How do I run the java application?
I am testing on my Windows 7 Professional 64-bit desktop, but I need to run on CENTOS too. That should be no problem, as I assume whatever the fix on Windows is, will work on CENTOS.
UPDATE:
I placed the two lib files in a /lib folder to match the answer given. I then issued the following command line and got the following error message.
C:\SoftDev\Projects\NetAccelerator\JavaWorkspace\NetAccelerator32>java -cp "bin/*;lib/*" PkgNetAccelerator32.netAcceleratorApp
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
at PkgNetAccelerator32.netAcceleratorApp.open(netAcceleratorApp.java:43)
at PkgNetAccelerator32.netAcceleratorApp.main(netAcceleratorApp.java:33)
I found the following article, which produces a command line that works, just that is not reasonable for sending. I much rather use the answer.
Lets assume you have a eclipse project structure similar to this:
Workspace
-> Project
-> src (source code directory)
-> bin (compiled byte code directory as JAR)
-> lib (jar directory)
-> .classpath (eclipse file)
-> .project (eclipse file)
I am assuming that you are exporting the JAR as an executable JAR.
So in order to run a class in that exported JAR with another JAR from the lib directory (swt.jar) while your working directory is the Project directory you will need to execute:
java -cp "bin/*:lib/*" PkgNetAccelerator32.netAcceleratorApp
Using wildcards requires Java 6 or later
Please note: CENTOS requires : as a path separator. Windows uses a semi-colon.
This will add your classes and the swt classes onto the classpath and run your main class PkgNetAccelerator32.netAcceleratorApp
SWT 32bit vs 64bit
You will either need to use a 32bit JVM locally or continue to use a 64bit JVM and use the 64bit version of swt; see swt on windows 64 bit. I would suggest you match your version of your local JVM (32bit or 64bit) with the version that you will be running on the CENTOS machine. You will however need a different swt jar for CENTOS.

Creating Lock file in Programs Folder causes exception

I recently decided to use an official installer for my Java application.
The application installs its self in the appropriate folder under the Programs Files.
Inside the bin folder where the jar for my application sits, I have a h2.db file which contains a bunch of information that is read by the application.
When I try running the application in the installed location I get an exception:
org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.FileNotFoundException: C:\Program Files (x86)\Aurora Game Hub\bin\AuroraDB.lock.db (Access is denied)"; "C:/Program Files (x86)/Aurora Game Hub/bin/AuroraDB.lock.db" [90031-167]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:158)
at org.h2.message.DbException.convertIOException(DbException.java:315)
at org.h2.store.fs.FilePathDisk.newOutputStream(FilePathDisk.java:265)
at org.h2.store.fs.FileUtils.newOutputStream(FileUtils.java:223)
at org.h2.store.FileLock.save(FileLock.java:197)
at org.h2.store.FileLock.lockFile(FileLock.java:333)
at org.h2.store.FileLock.lock(FileLock.java:128)
at org.h2.engine.Database.open(Database.java:542)
at org.h2.engine.Database.openDatabase(Database.java:222)
at org.h2.engine.Database.<init>(Database.java:217)
at org.h2.engine.Engine.openSession(Engine.java:56)
at org.h2.engine.Engine.openSession(Engine.java:159)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)
at org.h2.engine.Engine.createSession(Engine.java:121)
at org.h2.engine.Engine.createSession(Engine.java:28)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:305)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:110)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:72)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at aurora.engine.V1.Logic.ASimpleDB.searchAprox(ASimpleDB.java:828)
at aurora.V1.core.GameSearch.searchGame(GameSearch.java:249)
at aurora.V1.core.GameSearch.run(GameSearch.java:346)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\Program Files (x86)\Aurora Game Hub\bin\AuroraDB.lock.db (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.h2.store.fs.FilePathDisk.newOutputStream(FilePathDisk.java:257)
... 22 more
It works fine when its not in the Program Files location, as I would have noticed this while developing.
i'm thinking it has something to do with permissions and the lock file not able to be created or something. Is there a way to give/ask explicit permission to create the lock file or can I tell H2 to just not create the lock file?
We've thought about having the static database placed somewhere other then the install location, but since this has to run on Mac and PC and the way the installer is set up it would make things more complicated.
Any help would be very much appreciated.
It seems there are no access rights to write to this directory. You need to store the database file in a directory where you do have access rights.
For H2, if you use the database URL jdbc:h2:~/data/db, the database is stored relative to the current user home directory. Another alternative is to use an absolute path, for example jdbc:h2:c:/dir/to/db/file
This is caused by improved security in windows. You can write only when you have elevated privilages. For example if you start program as 'run as administrator', then it may able to write to Program Files area. Then it is not a good idea- see: Bypass Windows permission restrictions on program files folder
You should use folder pointed by 'ProgramData' variable. or good old user.home area
On Mac OS X, you can use the known subdirectories of user.home cited here.
Java Web Start with suitable permissions may be an alternative, although I haven't tried it.

Can not start java web start

I have typed javaws url_of_jnlp in my CMD under windows 7, but i got the following error:-
CouldNotLoadArgumentException[ Could not load file/URL specified: url_of_jnlp]
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main.access$000(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)
I have noticed this problem since i need java web start to be able to run an application, but when i tired to run this application , i will be asked to save a file named webstart.jnlp , instead of automatically lunching the java web start.
So what might be the problem?
You need to replace url_of_jnlp with the actual url of the jnlp file of the application.
This will not work: javaws url_of_jnlp
This will work: javaws "C:\..[Path to folder]..\My_Application.jnlp"
You should really have found this info yourself, it is available at the bottom of this page:
http://www.java.com/en/download/faq/java_webstart.xml
See this page for info on command line options for javaws: http://docs.oracle.com/javase/1.5.0/docs/guide/javaws/developersguide/javaws.html
Check the following links to make sure everything is perfect in your setup.
Common Java Web Start Problems
Deploying Java Web Start Applications
Hope it helps.

FileNotFoundException starting 1.4.2 webstart application with java webstart 1.6 over HTTPS

And many thanks to all who will take time for reading.
I'm struggling to make a webstart Swing 1.4.2_12 application to launch with java webstart 1.6.0_29.
Here is the scenario :
All traffic is done over HTTPS
User clics a link on a web page to launch the application : servlet
generated jnlp file
Java webstart client (1.6.0_29) starts and loads the application in
a breath
Because the j2se version is set to 1.4.2_12 in jnlp descriptor java
webstart 1.4.2_12 is used to launch the application (loaded by
1.6.0_29)
Java webstart 1.4.2_12 fails on startup claiming that it cannot find some ghost "javaws2" file
The following message is reported:
CouldNotLoadArgumentException[ Could not load specified file/URL : C:\DOCUME~1\BENOIT~1.VAT\LOCALS~1\Temp\javaws2]
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Full stacktrace is:
java.io.FileNotFoundException: C:\DOCUME~1\BENOIT~1.VAT\LOCALS~1\Temp\javaws2 (File not found)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.javaws.jnl.LaunchDescFactory.buildDescriptor(Unknown Source)
at com.sun.javaws.Main.launchApp(Unknown Source)
at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
at com.sun.javaws.Main$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The same error occurs when HTTPS is disabled.
Both the following HTTP headers are sent to the client by JNLP servlet :
Cache-Control : empty
Pragma : empty
These headers are necessary to prevent IE to disable caching in HTTPS mode wich prevents java webstart to find the loaded jnlp file (because it is not cached).
Mime-type is set to : application/x-java-jnlp-file
What I do not understand is why java 1.4.2_12 cannot find the jnlp file loaded by JWS 1.6.0_29 ?
Thanks for reading.
#Andrew : Thank you for your interest.
Here comes my conclusions on that issue.
In a nutshell : Always set the href attribute in the the jnlp tag !
<jnlp spec="1.0+" codebase="htt://myhost/codebase" href="mustSetThisAbsolutelyForJWS-1.4.2_12.jnlp">
...
</jnlp>
Multiple HTTP debug sessions have shown that :
Scenario 1: href attribute is left empty and JNLP files are associated with JWS 1.6 on the client
User clicks a jnlp link, JNLP file is downloaded and cached by
browser.
JWS 1.6.0_29 starts up then read the downloaded JNLP file.
JWS 1.6.0_29 downloads all JAR files and then switches to JWS 1.4.2_12.
JWS 1.4.2_12 starts up and then obviously read the JNLP file in its turn.
JWS 1.4.2_12 tries to retrieve the JNLP file pointed by the href
attribute.
Since href is empty, JWS reports "Could not load specified file/URL
: C:\DOCUME~1\BENOIT~1.VAT\LOCALS~1\Temp\javaws2". (As a side note, ghost javaws2 is sometimes named javaws10 or similar)
Please note : When using JWS 1.4 or 1.5 with this scenario, the
application WILL start. The issue only appears when JWS 1.6 is used
to run a 1.4.2_12 application. JWS 1.5 and 1.6 do not fail when no
href attribute is set. Instead they simply use the JNLP as is, check the
JAR updates and then launch the application.
Scenario 2: href attribute is set with a proper value and JNLP files are associated with JWS 1.6 on the client
User clicks a jnlp link, JNLP file is downloaded and cached by
browser.
JWS 1.6.0_29 starts up then read the downloaded JNLP file.
JWS 1.6.0_29 downloads all JAR files and then switches to JWS 1.4.2_12.
JWS 1.4.2_12 starts up and then obviously read the JNLP file in its turn.
JWS 1.4.2_12 tries to retrieve the JNLP file pointed by the href
attribute.
JWS 1.4.2_12 downloads the JNLP pointed by the href attribute.
JWS 1.4.2_12 downloads all JNLP JAR files referenced in the freshly downloaded JNLP file.
JWS 1.4.2_12 launches the application correctly.
It's been a major pain in the neck for me and took me days to figures this out. Hope this will help someone one day.

Categories

Resources