jar file with source code only - java

Suggest I have a HelloWorld.jar file, this file contains only HelloWorld.java , which is the source code of the application.
Will it be possible to run this jar file and execute the application, even though I don't have HelloWorld.class?

Yes, it can be done: See the javax.tools api. It is not easy, but it can be done....
You will likely be better off with a script that unjars the file, compiles it, and runs it.

Directly? No. java accepts only classfiles. To use source it must be compiled with javac. Nothing keeps you or a utility from compiling the source files to class files and using those, however.

Related

How to make VS Code recognize .class file?

I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there

What is the difference between the Apache sources.jar and .jar files?

I'm curious about Apache commons-io, why do they include a sources-jar inside the code package. We will not compile the program like so :
javac -cp .;.\lib\commons-io-2.4-sources.jar myCode.java
But we compile it like this :
javac -cp .;.\lib\commons-io-2.4.jar myCode.java
So why do the libraries also include a -sources jar in the download code ? I'm guessing it's for studying the source code, if we want to add/improve ?
The source JAR is so you can read the code is you want to. If you use an IDE, it can know to down load this JAR and if you look at a class in it, it will show your the source. esp useful when debugging a program. If you are not using an IDE, you can unpack the source and read it to understand what it is doing.
The reason the source is not included in the compiled JAR is so it can be easily dropped if all your are doing is running the program e.g. in production.

How to convert multiple class files to one jar file?

I have decompiled a jar file,
and made two classes from it. After that, I tried to make a new jar file with these two class files, using this code
jar cvf AB.jar WinRegistry.class StartPageChangeApplet.class
The file created without any errors. However, when I look at the source code on Java Decompiler, it says "Internel Error", means that I couldn't make the jar file properly.
Where am I doing doing wrong ?
Please define "made two classes from it". Which java compiler (e.g. javac.exe) are you using? Did you just copy the source to a .class file without compiling maybe?
The java decompiler JAD actually displays source code, not class bytecode. Don't get confused by the title of the editor which is saying WinRegistry.class.
So you can't just save that as a .class. You need to save it as a .java and then compile it to .class using a java compiler:
javac WinRegistry.java StartPageChangeApplet.java
jar cf AB.jar WinRegistry.class StartPageChangeApplet.class
From Eclipse, you can do this way..

Convert .class file to .java file

Is this possible to convert a .class file (from .jar external library) to a .java file? I'm trying to figure out whether it is possible or not because the source of the external library is unavailable.
What are the steps I need to take to do this?
use a java decompiler like "Cavaj". It will open the class into a txt format, copy the code to a file and save as .java
Use jad. Download it from here. It works fine with classes compiled up to SDK 1.4... 1.5, if I recall correctly.
The javap command takes class-names without the .class extension. Try
javap -c ClassName
javap will however not give you the implementations of the methods in java-syntax. It will at most give it to you in JVM bytecode format.
To actually decompile (i.e., do the reverse of javac) you will have to use proper decompiler.
http://download.cnet.com/Cavaj-Java-Decompiler/3000-2213_4-10071619.html
or may be this be of some help Java Decomilers
it is Possible to convert a class file to java file without using any tools . e.g Decompiler or something else ?

How to compile a single Java file

I have searched this, but I could'n find or understand what I found.
Now I'm not a Java programmer, but I have the need to compile a single Java file into an existing (compiled) Java program. The source of this Java code is not available to me, therefore I cannot compile the entire project.
I'm not interested in decompiling the original project.
How to quickly do this using only the JDK and javac? (Through the command line is what I prefer.)
I understand that to do so error checking outside of the single java file will have to be disabled, because it can't read the dependencies.
Thanks in advance,
-Aidiakapi
EDIT: I do have the JAR file, thanks for the answer :)
As far as I can understand you want to re-compile a single java file and replace it in an existing jar file..
So you compile it..
cmd>javac -classpath jar1.jar;jar2.jar my.company.MyClassToReplace.java
and replace it in the jar.
cmd>jar uf myJarFile.jar my/company/MyClassToReplace.class
You need to have the jar(s) which contains all the things your class depends on to compile it.
You can then compile the Class with
javac -classpath jar1:jar2 mypackage.MyNewClass
If you have no access to the original Jars, you will have to create mock classes and method etc (which don't have to do anything, just be there so your class compiles) Using an IDE can make both processes easier. (That is what it is for ;)

Categories

Resources