Decompile, edit and compile a .jar file - java

I am working on a program that automaticly decompiles and compiles java programs. The decompiling works now. But now i am stuck on the compiling of multiple java files in multiple packages. Can anyone help me to find acmd command for compiling a java project?
And if possible make it support external libraries
I hope i did not make that much grammar mistakes English is not my native
language ;-)

Lets say you are at the application root folder trying to compile App.java file from com.example package that uses lib1.jar and lib2.jar libraries from lib folder to a destination bin folder, compilation command should have the following format:
javac -d bin -sourcepath src -cp lib/lib1.jar;lib/lib2.jar src/com/example/App.java
So 'bin/com/example/App.class' file should be created. If App.java uses other classes from the project, they all should be automatically compiled and put into corresponding folders.

Related

Compiling a Net beans created project in Linux terminal

I created project in netbeans. In Netbeans it compiles. But i need to know the way of compiling the project in Linux. It simply not just a file. I have folder which contains Build,nbproject,src and etc.
Please tell me the way of compiling a project created in netbeans in linux terminal.
In the most basic case you compile all .java files in the src folder. This generates .class files. You then execute the main class file (which contains a main method). Don't forget to also put all other dependent classes on the classpath.
Compile:
$ cd <yourproject>/src
$ javac $(find . -name "*.java")
Run:
java -cp ./ yourpackagename.YourMainClass
the -cp argument specifies the classpath. All the classes on this path will be considered as dependecies. In this case we put the classpath to ./ which is the current directory (src). That means if MainClass uses other classes in your project, they will be linked (made usable). If you do not specify a classpath your whole application would need to be contained in your MainClass, which is basically never the case.
Another way - much more common - you create a jar archive (basically a zip file) which contains your compiled classes. I'm pretty sure you can generate jar files within NetBeans. That generated jar file can then be executed:
$ java -jar myjarfile.jar
Most people do not directly use the IDE in order to generate the jar but rather use build tools such as Gradle or Maven.
Another option is using the jar command:
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Java creating .jar file
Hope this helps.

Eclipse Java project: can I set eclipse to store .class and .java files in 1 folder?

I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.

Integrating Jasmin into Ant and Eclipse

I'm working on a Java project for which I need a very specific testing code, which I create in Jasmin, the JVM assembly. The project is built with Ant and has a nice directory structure, namely:
root
/src
/classes
/tests
/build
/classes
/tests
The tailor-made Jasmin source file is in the src/tests directory, together with the JUnit Java files. All of these get built with Ant (I use the simple "java" Ant task to run the Jasmin compiler on the source file), they're put into respective directories inside the /build folder, and Ant then runs the JUnit tasks. All of this works fine...
Until I introduced the single Jasmin-generated class, Eclipse worked perfectly with the project. The problem is that Eclipse doesn't see the new .class file. The Build Path contains src/classes and src/tests as source folders, and /build as an output folder. Yet it doesn't seem to be able to find the class generated by Jasmin, so there are hundreds of "ClassX unresolved" problems and it really makes developing quite painful.
Does anyone have an idea how to force Eclipse to see this class?
A simple solution would be to compile Jasmin classes into a separate class folder and then add that folder into Eclipse's build path: Project Properties / Java Build Path / Libraries / Add Class Folder...

I cannot include the jar files in my project

I tried to use a sourceforge library in my program.
On my computer I use Eclipse, and I easily add the jar files to my project.
Now, I want to move the code to another computer.
I tried an executable jar file, but the problem is I cannot debug it on the new computer.
So, I decided to move the source code and compile it there.
I tried the following but defeated in all of them: (all in Windows Command prompt)
Copy the jar files in the /lib/ext folder of my jre folder and add this folder to classpath
javac -cp ".\lib\*.jar" src/*.java
javac -cp "./lib/*.jar" src/*.java
In all of them the classes that are defined in the library jar files can not be recognized by java!
Actually the package doesn't find...
Any idea? Any stupid thing that I am doing?
The correct wildcard for matching all jars in a directory is just
-cp "dir/*"
Please see the Understanding class path wildcards section of this page: Setting the classpath

compile a java project with different packages

I have a java project in eclipse with different packages. How can I compile the project from command line in unix (bash) ? Using plain javac doesnt seem to work. For eg, in eclipse I have
src
...server
...client
...shared
And the main file that I want to run later is in the server package called server.java
You will have to add all the source paths and dependency .jar files into the compilation call. The easiest way to do this will be to get Eclipse to export an Ant build.xml for the final jar you are trying to compile. This autogenerated ant file is usually pretty messy but gives you an idea of how the project should be built.
javac -sourcepath /path/to/src -d /path/to/classes

Categories