I am trying to put a java program into a HTML page and when launch the page nothing shows up.
How do I call this applet, the only thing I have is a JAR file, I didn't write the program. Thanks all!
edit1: Also I kinda got something going but now it says missing code parameter.
You'll need either a CODE attribute or an OBJECT attribute.
From the Oracle guide to embedding APPLETs:
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.
OBJECT=< serializedApplet >
This attribute gives the name of the file that contains a serialized representation of an Applet. The Applet will be deserialized. The init() method will not be invoked; but its start() method will. Attributes valid when the original object was serialized are not restored. Any attributes passed to this APPLET instance will be available to the Applet; we advocate very strong restraint in using this feature. An applet should be stopped before it is serialized. One of CODE or OBJECT must be present.
More, and a tutorial here:
http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
Related
I want to implement a function that will take package name as input and list all the contents(only files) inside that package.
public List<String> getContents(String packageName) {
...
}
Example input
packageName = com.spring.com.demo
Expexted output
Class1.java
Class2.java
...
What is the best way to accomplish this task?
You're talking about java's 'classloader' system, which is a slight misnomer, in that it can also load resources other than classes. Point is, classes are a resource that are located somewhere, and the JVM itself needs to find them. After all, when you are running your main class and it refers to 'new Foobar()', the jvm needs to find Foobar.class somehow, read the bytes, and turn them into a new class definition.
Java's classloader system is generalized in two ways:
You can call on it to find you stuff.
You can for example write:
MyApp.class.getResource("icons/share.png")
and get a URL object you can pass straight to e.g. ImageIcon. This way, you can ship your icons for your GUI app together with your class files, and it's completely unified: However the system is loading the classes, be it from disk, from inside a jar file, from eclipse's module system if it's an eclipse plugin, or from the network - this way you're loading your images from the same place.
You can make your own
Let's say you want to write a system that loads classes (and other resources, like images) directly from the web. You can do that: You can make your own ClassLoaders.
Now for the crux of the matter
That means ClassLoader is an abstract concept that lists which features it has. It's like any other interface / abstract class in that regard: It's a template that defines what you can do with one, so that anybody can provide you with an implementation of it.
Here's the crucial thing you must understand so that you know why what you want is impossible (and why the commonly called out 'reflections' library is a hack that doesn't universally work):
The ClassLoader abstract simply has no list command.
Hence, listing? Simply not possible. The only command it does have is 'load Resource X', X being some path-like string. That is all it has. The java classloader system is never in need to 'list all classes in a package', not even when there's a star import (which is just a thing javac knows about, at the class file level star imports aren't a thing). The JVM just needs to e.g. load resource '/java/lang/String.class' - hence, the command 'here is a path, please give me the bytes for it' is all that is neccessary.
The solution to have lists anyway
During compilation, the abstractions do support 'list'. After all, the compiler really does just read java files from a directory (which supports 'list all files inside it'), you can't for example tell the compiler 'please compile all .java files in this .jar file). So, at compile time, you can make a list of resources.
So here's the trick: Save that in a text file and ask for the text file during runtime. Then translate each line in the text file to the full resource path and then ask for each of those. Thus, using just the one API call you have available to you at runtime ('load resource at path X'), you can have a list system anyway. It's just that during the compilation/build/packing step you need the tools that compile/build/pack to do some work and make you a text file with the details.
This is called the SPI (Service Provider Interface) system and java itself uses it - it's how JDBC drivers and e.g. charset implementations are found.
You can use this yourself in this three step process:
Define an interface or abstract class that serves as the thing 'providers' will implement/extend. Let's say it is com.ranjan.MyService`.
At the provider end: Write an implementation for it. Let's say it's com.zwitserloot.ServiceImpl.
During compilation, ensure that in the same place the class files end up (e.g. in the jar file for example), there's META-INF/services/com.ranjan.Myservice (a text file). This file contains the fully qualified class name of each 'provider' class: com.zwitserloot.ServiceImpl is what's on the first (and only) line.
At runtime: Use java.util.ServiceLoader and it'll take care of everything.
Your specific use case
From comments it seems like you don't really need to 'list classes', you have a bunch of classes and need to know if they are 'valid', for some definition of 'valid'. You can either just Class.forName them, though this will also initialize them (run its static initializers). Alternatively, you can run YourOwnClass.class.getResource("com.spring.com.demo.Class1.class"), and check that [A] you get an actual resource (and not null, indicating it doesn't exist), and [B] do whatever validation you want to do on this. For example, toss it through bytebuddy or ASM or some other class format editor and check if it can parse it.
I'm working on an Eclipse plugin that enables traceability. I am implementing a notification system that tells the user whenever a traced item changes (is removed, renamed or edited) and for that purpose I implemented an IResourceChangeListener, but that doesn't give me all the support that I want for Java elements.
For example, when I rename a Java method inside a .java file, it only tells me which file has been edited, but I would like to have the info about the method as well. I know that this can be achieved with implementing the IElementChangedListener, but is there any way around it? Do I really have to implement two listeners (ResourceListener for other files and ElementChangedListener just for java elements) or can I somehow get the IJavaElementDelta (normally obtained from the ElementChangedListener) from the IResourceDelta? Thanks!
These two deltas are completely unrelated. You need to use both listeners.
Try to check this link example 5. There is some method with this description:
Converts an IResourceDelta and its children into
the corresponding IJavaElementDeltas.
Return whether the delta corresponds to a resource on the classpath.
If it is not a resource on the classpath, it will be added as a non-java
resource by the sender of this method.
So I suppose it could be possible.
The links leads here which should you check ass well. Method public void processJavaDelta(IJavaElementDelta delta)
So I learned about the getCodeBase() method today in my programming class and to be honest, I'm really interested in it and how it works in general. All I know is that it finds the directory of where the source code is stored and it is mostly used in applets. But anyone explaining, in detail, how it actually works like the technicalities and such, I would be super grateful! By the way, I started Java 6 months ago. Thanks!
This is what I learned, just to put a little code in here.
cards[0] = getImage( getCodeBase(), "cards_gif/1.gif" );
getCodeBase() in this case, is used to initalize a part of an array to a image.
It returns whatever the codebase was that was specified in the HTML, or its default.
getDocumentBase()
Gets the URL of the document in which this applet is embedded.
For example, suppose an applet is contained within the document:
http://java.sun.com/products/jdk/1.2/index.html
The document base is:
http://java.sun.com/products/jdk/1.2/index.html
getCodeBase()
Gets the base URL. This is the URL of the directory which contains this applet.
I want to delete this post, but can't do it as it's accepted.
I am currently reading a book on java, and I am currently studying the swing graphical user interface components. While I was doing so, I stumbled upon a code example, where the author was setting an image on a JButton with a very unusual way, depicted below:
Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) );
In order for the above to work, you need to have the image on the same folder as the .class files. Can someone explain to me why is he using this particular code (which as far as I know, it must be reflection code, but then again, I am not particularly sure about this one) and if there is one way for me to do the same thing, without getting things as complicated as he does?
Things are complicated only if you don't understand them. Once you have understood what the above code does, it will be extremely simple.
getClass() returns the Class object of the current object (this). getResource() called with a relative path as above, looks for a file, in the classpath, named bug1.gif, and in the same package as the Class object being called. So it looks for bug1.gif in the same package as the class containing the above code. getResource() returns a URL. And the ImageIcon constructor takes a URL as argument, loads the image bytes from this URL, and constructs an ImageIcon from these bytes.
So the whole thing just creates an ImageIcon from a file available from the classpath, in the same package as the class calling this code. And this makes sense: you put the images used by a given class in the same package as the class, and you release a jar containing the classes and the images of the application.
You would have figured all this by yourself by reading the javadoc of all these methods.
Java's Swing can get over complicated really fast and I think he's actually using this code for simplicity.
If the image path is relative (the path in your example is), the image has to be located in the same location as the compiled byte code of your program, the .class files. If the image was anywhere else, your program simply couldn't find it.
Relative paths like this are very useful especially when you want to compile your finished project into a JAR file. Your image will be included in the JAR with all your .CLASS files. You will be able to download your compile program, run it, and your images will be right there in your GUI as you would expect.
Class#getResource(String) returns an URL to resource from the classpath. This is a convinient way of loading resources that are stored inside your application JAR file. If the image lies somewhere on the HDD, you can load it using:
new ImageIcon(new File("/path/to/the/image").toURI().toURL());
which creates the File object, and gets it's path as URL (which will look like file:///path/to/the/image).
Or even easier, as ImageIcon has a contructor that takes a fileName:
new ImageIcon("/path/to/the/image");
I'm having an applet in which the class name is known but the text box fields and other controls and their ids are not known.
Now i need to get the id of the textbox or the total controls exists in the applet from javascript or some java code.
If you're trying to say the text boxes (and whatnot) are part of the Java applet -- these are not HTML elements (although they may look similar), so they are not directly accessible from JavaScript. The applet may provide indirect access to them via LiveConnect, but by default there is no interface provided.