How to compile all Java source code including inside folders and subfolders?
The javac command allows you to specify the files to be compiled by listing their pathnames on the command line, or by giving a file (command line syntax "#file") that contains a list of source filenames. In either case, the way you generate the list of filenames will be OS specific. For example, on Linux you would typically use shell globbing or the find utility to build the list; e.g.
javac <options> */*.java
or
javac <options> `find . -name \*.java`
or
find . -name \*.java > list
javac <options> #list
or something similar.
However, if you have a number of files to compile, you would be better off in the longer term using a Java build tool such as Ant or Maven. In the Ant case, you specify the files to be compiled (etc) as a FileSet using patterns (aka an antpaths) to match the files. In the Maven case, the build tool typically figures out the Java source filenames are for itself, based on your project's directory structure.
Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. Java byte-code is a platform independent version of machine code; the target machine is the Java VM rather than the underlying architecture. To compile a Java source code file Foo.java, you would do the following:
% javac -g Foo.java
The -g command line option is optional, but I recommend using it as it makes debugging easier.
But why do not use an IDE to handle all this. E.g. eclipse or netbeans. There you can manage your source code and build it.
If you use the Maven build tool for Java, it has the ability to automatically compile all the Java sources in a folder and its subfolders; otherwise, you pretty much have to invoke javac with the path to each source file. For an example Maven-based Java project, see the Java Project Template. If you download the template at the link, you can simply use make or mvn package to compile all the java sources into an executable jar file. Note that this requires you to install the Apache Maven2 build system.
Related
I need to translate itext source jar to objective c using J2ObjC.
I use the Xcode Build rules at here .
But when I add this script
"${J2OBJC_HOME}/j2objc" --build-closure -d ${DERIVED_FILES_DIR} -sourcepath "${PROJECT_DIR}/" --no-package-directories "${PROJECT_DIR}/Classes/Othello/Engine/itext-2.1.7-sources.jar" ${INPUT_FILE_PATH};
I get build errors
This project was built and run successfully without my script.
Please help me
Thanks
Try removing the last ${INPUT_FILE_PATH}. j2objc's flags are modeled after javac's (to support build tools), so any argument after the last flag is treated as a source file. Since the sources jar holds all the source files, the ${INPUT_FILE_PATH} tells the compiler to compile a directory, which it can't do.
IDEA has a great built-in feature - decompiler. It works great.I can copy source code, but I cannot find option to extract all decompiled java classes to java files.
This project has a lot of java classes and packages, so I will be to long to copy this manually.
Does anyone know how to extract to java source files.
Thx
As of August 2017 and IntelliJ V2017.2, the accepted answer does not seem to be entirely accurate anymore: there is no fernflower.jar to use.
The jar file is called java-decompiler.jar and does not include a main manifest... Instead you can use the following command (from a Mac install):
java -cp "/Applications/IntelliJ IDEA.app/Contents/plugins/java-decompiler/lib/java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler
(you will get the wrong Usage command, but it does work).
Follow instructions for IntelliJ JD plugin. Or see an excerpt from the instructions below.
java -jar fernflower.jar [<source>]+ <destination>
+ means 1 or more times
<source>: file or directory with files to be decompiled. Directories are recursively scanned. Allowed file extensions are class, zip and jar.
<destination>: destination directory
Example:
java -jar fernflower.jar -hdc=0 -dgs=1 -rsy=1 -lit=1 c:\Temp\binary\ -e=c:\Java\rt.jar c:\Temp\source\
Be aware that if you pass it a ".jar" file for the source, it will create another ".jar" file in the destination, however, within the new ".jar" file, the files will be .java instead of .class files (it doesn't explode the jar).
UPDATE
People ask me: How do I get the fernflower.jar?
If you have any IntelliJ product installed, chances are that you already have the Fernflower decompiler on your computer. IntelliJ IDEA comes with Java Bytecode Decompiler plugin (bundled) which is a modern extension of Fernflower.
Locate the file in ${IntelliJ_INSTALL_DIR}\plugins\java-decompiler\lib\java-decompiler.jar (example: C:\Program Files\JetBrains\IntelliJ IDEA 2018\plugins\java-decompiler\lib).
Copy it somewhere and rename to fernflower.jar (optional).
This JAR is not executable, so we can't run it using java -jar. However something like this works:
java -cp fernflower.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler [<source>]+ <destination>
org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler is the class that contains the main method to run the decompiler.
Example:
mkdir output_src
java -cp fernflower.jar org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -hdc=0 -dgs=1 -rsy=1 -lit=1 ./input.jar ./output_src
If you don't have IntelliJ products installed, either download it now (available on jetbrains.com) or make your own decompiler executable from sources (available on Github).
Open an existing project or create a new one.
Go to Project structure settings > Libraries. Add the jar you want to decompile in libraries by clicking the + symbol.
Go to the Project tool window shown on the left. Search for jar name that you added in the previous step. Navigate to the desired class or package.
You can see the decompiled java files for that jar.
You could use one of these (you can both use them online or download them, there is some info about each of them) :
http://www.javadecompilers.com/
The one IntelliJ IDEA uses is fernflower, but it can't handle recent things - like String/Enum switches, generics (didn't test this one personally, only read about it), ...
I just tried cfr from the above website and the result was the same as with the built-in decompiler (except for the Enum switch I had in my class).
Try
https://github.com/fesh0r/fernflower
Download jar from
http://files.minecraftforge.net/maven/net/minecraftforge/fernflower/
Command :
java -jar fernflower.jar -hes=0 -hdc=0 C:\binary C:\source
Place your jar file in folder C:\binary and source will be extracted and packed in a jar inside C:\source.
Enjoy!
The decompiler of IntelliJ IDEA was not built with this kind of usage in mind. It is only meant to help programmers peek at the bytecode of the java classes that they are developing. For decompiling lots of class files of which you do not have source code, you will need some other java decompiler, which is specialized for this job, and most likely runs standalone, like jad, fernflower, etc.
Some time ago I used JAD (JAva Decompiler) to achieve this - I do not think IntelliJ's decompiler was incorporated with exporting in mind. It is more of a tool to help look through classes where sources are not available.
JAD is still available for download, but I do not think anyone maintains it anymore: http://varaneckas.com/jad/
There were numerous plugins for it, namely Jadclipse (you guessed it, a way to use JAD in Eclipse - see decompiled classes where code is not available :)).
I use JD-GUI for extract all decompiled java classes to java files.
Someone had gave good answers. I made another instruction clue step by step.
First, open your studio and search. You can find the decompier is Fernflower.
Second, we can find it in the plugins directory.
/Applications/Android Studio.app/Contents/plugins/java-decompiler/lib/java-decompiler.jar
Third, run it, you will get the usage
java -cp "/Applications/Android Studio.app/Contents/plugins/java-decompiler/lib/java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler
Usage: java -jar fernflower.jar [-<option>=<value>]* [<source>]+ <destination>
Example: java -jar fernflower.jar -dgs=true c:\my\source\ c:\my.jar d:\decompiled\
Finally, The studio's nest options for decompiler list as follows according IdeaDecompiler.kt
-hdc=0 -dgs=1 -rsy=1 -rbr=1 -lit=1 -nls=1 -mpm=60 -lac=1
IFernflowerPreferences.HIDE_DEFAULT_CONSTRUCTOR to "0",
IFernflowerPreferences.DECOMPILE_GENERIC_SIGNATURES to "1",
IFernflowerPreferences.REMOVE_SYNTHETIC to "1",
IFernflowerPreferences.REMOVE_BRIDGE to "1",
IFernflowerPreferences.LITERALS_AS_IS to "1",
IFernflowerPreferences.NEW_LINE_SEPARATOR to "1",
**IFernflowerPreferences.BANNER to BANNER,**
IFernflowerPreferences.MAX_PROCESSING_METHOD to 60,
**IFernflowerPreferences.INDENT_STRING to indent,**
**IFernflowerPreferences.IGNORE_INVALID_BYTECODE to "1",**
IFernflowerPreferences.VERIFY_ANONYMOUS_CLASSES to "1",
**IFernflowerPreferences.UNIT_TEST_MODE to if (ApplicationManager.getApplication().isUnitTestMode) "1" else "0")**
I cant find the sutialbe option for the asterisk items.
Hope these steps will make the question clear.
2022 UPDATE
Like any experienced developer who's being honest, I have to admit it -- I'm lazy. More is less and less is better when it comes to typing. The answers from #naXa and #yan are fine, except for having to open a terminal and type :-(
To decompile a jar right from within intellij, create a reusable JAR Application run configuration:
Copy java-decompiler.jar to a folder in your project (I used lib). Get from:: Windows: C:\Program Files\Intellij\plugins\java-decompiler\lib; or from github (documentation is here)
Copy source jar or class files to same folder
Create a run configuration with settings like these:
Path to JAR: browse to java-decompiler.jar
Program arguments: -hdc=0 -dgs=1 -rsy=1 -lit=1 your.jar lib
Working directory: lib
JRE: 11
In this case, your.jar is the source and lib is the output directory. Save it, run it, and watch it do its thing :-)
Need to do it again with different class files or jars? Duplicate the run config and update Program arguments.
---- package jar (java) jar -cvfM0 test.jar
---Decompile jar(class)>>>>>>>>>jar(java) java -cp "C:\Program Files\JetBrains\IntelliJ IDEA 2020.1\plugins\java-decompiler\lib\java-decompiler.jar" org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler -dgs=true C:\ Users\luo\Desktop\sf1\pxks\test.jar C:\Users\luo\Desktop\sf1
Unzip manually
Or use the following command to decompress
find the path
cd..
decompress
jar -xvf test.jar
Reference http://t.csdn.cn/3YLK4
I have been researching this for weeks and can't seem to get it figured out.
I have a Java program that I have written using NetBeans. It has several imports or .jar files it relies on. It runs fine in NetBeans. But I can't figure out how to call the .jar files and compile from the bash command line. I am using a Mac. I have read several posts on this and none so far have made sense to me. There are 26 imports being used in the program. I don't know if I need to use Ant or specify -CP or Classpath to compile. Surely I don't have to type each one of the .jar files out to compile this from the bash command line?
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
You should be able to simply use ant with the build.xml in the Netbeans project directory. It should "just work" for you. The project will likely not build WITHOUT Netbeans installed (if you tried moving the project to a different machine, for example), but with Netbeans, it should work out of the box.
If you don't have Ant installed, you'll need to install it.
Addenda:
To build it, if you have Ant installed, you should be able to simply go the project directory, where the build.xml file is, and type "ant", and it should build and put stuff in the dist directory.
If you go in to the dist directory and type java -jar yourapp.jar, it should run, because the manifest in the jar will point to the nearby lib jar files. If you want to distribute the app, there are different options for java, such as making a Mac compatible application, or a Windows EXE, you'll need to search for those. Or you can simply distribute the contents of the dist directory and write a script to do that whole java -jar yourapp.jar command.
If you are using Java 1.6 or above, you can toss all 26 jar files into a folder and simply add
-cp folder_name/*
as your classpath argument.
Some other options are 1.) type all 26 jar files on the command line (using the -cp argument as you have mentioned) 2.) use Ant or Maven or some other build tool and list those 26 jar files in the config file for said build tool or 3.) write a quick-and-dirty shell script that will set the CLASSPATH environment variable for you and then run your javac command.
Though it's probably reccomended one uses and IDE for coding advanced java projects, I personally prefer running almost entirely command-line (using gedit as a text-editor). So please don't just tell me "Just use eclipse!" or something :P
My question is what the method of creating a package in java is by a command.
I'm not talking about packaging an application that runs in the command line, I'm talking about making a package in the command line. Am I making a text file? Am I making a directory?
Relatedly, how does one link to related libs and natives without use of an IDE?
I know I'm being really awkward here, but I really prefer the control one gets working in command line.
There are three parts to it: (1) create directory structure; (2) indicate package in java file; (3) compile it.
For example, if you want to create package com.mycompany.myproject, then you need to start in the base directory for your project and then:
(1) create directory com/mycompany/myproject
(2) create java files in that directory, stating package com.mycompany.myproject in them;
(3) compile the files, for example, with javac -cp . com/mycompany/myproject/*.java
You may want to specify a different output directory so as to not mix sources and compiled classes.
If you need to use external libraries (.jar files) to compile, then you need to use -cp or -classpath command-line parameter to javac tool to specify them, e.g.
javac -cp .:some_library.jar:lib/another_library.java com/mycompany/myproject/*.java
It may be a good idea to put all external libraries in one place, e.g. lib subdirectory of your main project directory. And, by the way, the above javac command assumes unix-like environment. If you're on Windows, then you'll need to use ; for path separation.
packages are just directories on the filesystem.
so your package: com.mycompany.util corresponds to a directory com/mycompany/util.
When running and compiling etc your current workding directory should be where that top directory is located.
To include libraries, include them in your classpath when compiling and running. For example make a Project directory myproject and under that have your java-files and packages under myproject/src/ and libraries that you use under myproject/libs/
Then when your current workding directory is myproject execute java -cp .:libs/*.jar or the same with javac.
But I suggest you look into using ant or maven.
You can get along just fine on the command line by using a packaging tool such as Ant or Maven. Maven is especially handy because it is a higher level tool that already knows how to build various project types: command-line apps, webapps, libraries, etc. It also handles library dependencies by downloading them from repositories.
Java Package is just a directory structure, so a simple way of creating a Package lets say com.organization.test in terminal will be
mkdir -p com/organization/test
// to create a new directory and a subfolder that will be your package
$ mkdir -p parent/child
// to move into it :
$ cd parent/child
//to create an empty java file
$ touch MyClass.java
//to edit current file
$ nano MyClass.java
package child;
public class MyClass {
}
PS: The directory structure on your computer is related to the package name. That means when you edit .java file it needs to have a package declaration(your package) otherwise you will have a default package (ex: java.util.*).
I am new to Java. I was given some .java files and was told if I could convert them, then I could use them. It is for a game bot I am using. They changed to a new API which I don't understand, so the bot won't run unless it is implemented in .class files.
Can someone explain me how to convert these .java files to fully functional .class files?
You need to compile the .java source code to the .class byte code. Use a Java compiler like javac from Sun's JDK, or if you're using an IDE like Eclipse, it already has a compiler that can generate the .class files for you (usually in the \bin directory).
See also
Wikipedia/Java Development Kit / javac
http://www.eclipse.org/
Related links for javac
Getting Started With Java Tutorial: "Hello World!" / Common Problems (and their solutions!)
The Java Programming Language Compiler - javac
Manual page: Solaris/Windows
You need to compile the java using javac (the java compiler) You can get it as part of the JDK which you can get from Oracle (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
You'll need to run a command like the following
javac foo.java
Which will produce a corresponding foo.class
if you want to compile multiple java files into class files then you follow given procedure.
Syntex:
javac <options> <source files>
Example:
javac -cp -d classfolder *.java
here
options are -cp (copy), -d (directory), classfolder (directory name)
source files are * (all) .java files