J2ObjC with source jar file - java

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.

Related

Compiling source code makes .jar significantly smaller?

///WARNING- I DO NOT KNOW ANY LANGUAGES/HAVE NO USED ECLIPSE BEFORE\\
I'm currently trying to get a .jar file compiled in Java8 to compile in Java7, but seem to be having trouble, I have the source code intact so that is not the problem. It compiles fine, but the finished .jar is smaller than the original by a large margin. I believe the problem lies in my inexperience with Eclipse meaning I problably did something wrong.
Source Folder Structure
src\com\darkblade12\enchantplus...java files
Contents Of Each Folder
If you have some doubts about eclipse being behind the problem, try to compile it directly using the Command line Interface with the target version version of java by using javac.
Example for Windows:
set JAVA_HOME=C:\Program Files (x86)\Java\jre1.7.0_21
javac com/darkblade12/enchantplus/*.java
jar cvf program.jar -C com/darkblade12/enchantplus
Then compare your jar generated by eclipse and this generated directly from the command line.
Another way to verify is to open the jar file with and archive manager like 7-zip and verify its content.

How to decompile to java files intellij idea

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

how to run java program from flash drive that has classes in package, from command line?

I have an assignment where I have to submit my code in flash drive.
I did all coding in eclipse and getting all output. I tried running it through command line and it works. I copied all java files and class files to flash drive and tried again through command line it works in my laptop. but when I take that dive and put it my desktop computer and run through command line, it can not recognise the classes that are in the package.
I have a package a1.cis568 under this package I have several classes. main class is A1 and other classes are Circle, Point, PlaneCircle, EHashtable, CHashtable in the same package.
I have to use following line on command line to compile and run my program through flash drive, (don't have to change any classpath while doing so)
E:>
E:>javac -d . A1.java
E:>java a1.cis568.A1
When I was searching for solution I found that I can install JDK on flash drive and run the code. I tried installing JDK but it doesn't solve my problem.
If you are using eclipse you can easily export the project as a runnable jar. Copy the jar file and source to your flash drive. Execute from command line via the following
java -jar [runnable jar filename]
You seem to have troubles compiling your source code!
Since you navigate trought the package hierarchy to where A1.java is located and then try to compile, the compiler cannot find other classes which A1 depends on and which are not in the same directory. You have to tell it where to find them, this done using javac's option -sourcepath.
So from the same location of the class A1.java you could compile your code using something like this:
E:> javac -d . A1.java -sourcepath ..\..\
Here ..\..\ is used to tell that the sources can be found two directories above the actual directroy (based on your package tree a1\cis568\).
But to make things easy I would suggest you compile from the root of you source code tree. In this case the sourcepath and classpath are the actual working directory per default.
So navigate in the commad prompt to the location where the package/directory a1 is, then compile like this:
E:> javac -d . a1\cis568\A1.java
The comipiler should be able to find all classes on which A1 depends on (assuming they all exist with the correct package inside the root of your source code).
Run with
E:> java a1.cis568.A1

Compile and export in java via Terminal

I have a Java program in Eclipse on Mac currently, and I normally have to use multiple clicks just to export my code into a .jar file to test on my server. I would like to automate the process via terminal.
Basically, I compile my code usually by selecting the project
Export as Runnable JAR file
Select library handling: extract required libarires into generated JAR
Select export destination and hit done.
How can I do this via terminal? I assume this would first require me to compile the Java file, then to convert it to jar is a whole another step.
Help would be much appreciated.
You can create shell script that does it. This technique is obsolete since ~1998. So, use one of popular build tools. If you are starting now take a look on Gradle. Although there are a lot of other tools: good old ant, maven, buildr, ivy etc.
You can script all these activities using a build script. Several libraries exist for this, but Apache Ant is a good place to start. Ant build scripts can be run from command line or within eclipse, and will do all compilation, packaging and (some) deployment for you with a single command.
http://ant.apache.org/
Create default entry point manifest file as in : http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Open Terminal and create an executable jar file like this:
Sample Script:
#!/bin/bash
# set CLASSPATH if needed
cd workspace/src
javac -d . *.java
jar tf exported.jar .

How to compile java sources?

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.

Categories

Resources