How to compile a single Java file - java

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 ;)

Related

Compile Java source to another directory

so I'm fairly new to Java programming and I can compile and run Java code from the Linux terminal using javac objects.java and java objects. Although, I was wondering on how I could compile the source code to another directory? Let's say I have the file person.java in the /home/alarm/projects/src/ folder, but I want to compile it into the /home/alarm/projects/bin/ folder while still in my current directory. Not sure if that makes any sense, but can anyone explain this if possible? Btw, I am using Arch Linux just so you know my OS.
You can use the parameter -d to specify the target directory.
More information can be found here.

jar file with source code only

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.

Imports, jars, and heart Ache

first off let me start by saying I am completely new to Java, but to give you an idea of how new; I started reading lots of books, examples and so forth and began programming Java using Eclipse about 2 months ago. However, I found a really cool bit of advise about using notepad and the terminal to program instead. Kinda crazy for a newbie to go the hard route, but I love a challenge and I'm serious about learning.
So, In Eclipse I had a really good grasp of how to import, add jars compile etc. When I started using pico and using the terminal (I'm running ubuntu) to compile all went really well, until I wanted to use packages. I've spent two days pulling my hair out because no matter what I do I can't figure it out.
I'm trying to use the acm.jar (which I have many times in Eclipse) however I'm completely lost on how to use it when compiling from the javac in terminal.
So what I'm asking for, is for someone to explain the process getting my jar file to work.
All I'm using to create my java programs is the pico (or notepad) and the javac in the terminal.
To compile and run a java class using external libraries, you have to add that library to the classpath. The classpath is the set of places where the java compiler and the JVM look to find any external libraries/classes that it needs during the process of compiling/executing.
Setting the classpath can be done in 2 ways:
Set an environment variable called CLASSPATH
Set it when your run javac/java
Setting the classpath when running javac/java is done like this:
javac -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass.java
To run:
java -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass
: is used as a separator on Linux, for windows use ;
Assuming your source files are in src. Assuming you want your compiled classes to be in classes. Assuming your source files reference classes that are in lib/acm.jar:
javac -cp classes:lib/acm.jar -d classes src/com/foo/bar/MyClass.java
will compile the class com.foo.bar.MyClass and put the generated MyClass.class file in classes/com/foo/bar.
You need the acm.jar file in the classpath. That's what the -cp lib/acm.jar option does. You also need classes in the classpath, because MyClass probably references other classes that you have already compiled and that are in your classes directory.
To run your class, it has to be in the classpath, and acm.jar as well:
java -cp classes:lib/acm.jar com.foo.bar.MyClass
As you see, the classpath contains jar files, and directories containing the folder hierarchy which matches the package hierarchy.
I wouldn't use javac from the command line directly, though. Try using a real build tool, that will build all your classes at once, like Gradle or Ant. Maven is also very popular, but I hate it with passion.

Java and compiling when not using a IDE

I understand in java that you are forced into a single file per class.
So if I have classes like:
/my_project/main.java
/my_project/classes/user.java
/my_project/classes/other.java
And my main.java references the user and other files, how would I compile this via the command line?
If I was to have external .jar's that I was referencing, and I placed them in a particular folder, how could I also include this in my compiling? (or is there a general place I can put them where they will be picked up automatically like how python does this)
to compile, you will need to specify each source file, from the my_project folder:
javac classes/user.java classes/other.java main.java
You can also specify jar files for your classpath with the -cp option:
javac -cp myjarfile.jar main.java
You may also need to fiddle with the -cp flag to make sure your classes folder is in the classpath.
First of all it's poor style to make Java classes starting with lowercase.
Only public classes need to be in their own file, but you can add as many package-private classes as you like to the same file (although this is seen as poor style).
That said, the easiest way would to compile your code would be:
javac /my_project/main.java /my_project/classes/user.java /my_project/classes/other.java
In any case, proper code layout should be that classes are in a directory structure matching their package.
EDIT: There is a fairly good explanation of conventions here http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html
In addition to the above answer, you can use something like Apache Ant, for easier configuration of your build (if it gets complicated).
Look at the documentation for javac. You can pass multiple source files, or specify the source directory.

How does one build the java JRE from source (src.zip in JDK)?

Surprisingly enough I couldn't find the answer to this question.
I am trying to rebuild the java JRE from source. I obtain the java JRE source by extracting the src.zip file in the JDK.
After making any changes I need to make to the JRE, how do I compile the new source back into .java files (after which I can compress it into the rt.jar file).
Thanks.
You have better chances using OpenJDK (the base of Oracle/ Sun's future JDKs).
http://openjdk.java.net/
But what do you want to change actually? Maybe there is a better way...
Some of the Java sources that make up
rt.jar are generated during the build
process, from scripts and other means.
Some of the properties files are also
generated this way, and some of the
properties files are turned into Java
source that also contributes to
rt.jar. So without doing a complete
build first and populating the
'gensrc' directory, you won't have all
the sources that make up rt.jar.
Taken from:
http://www.java.net/forum/topic/jdk/java-se-snapshots-project-feedback/it-possible-just-build-rtjar
So when you say javac on all the java files inside src.zip it won't compile as the dependency graph is broken (missing generated files)
Also have a look at this: Where to get full source code for rt.jar?
If you want to change a number of class, you only need to compile those classes. You don't need to compile the whole JDK unless you intend to replace it.
If you just want to patch it, create a JAR of your changed classes and add this to the boot class path.
After revisiting the question. Javac on any of those files will allow you to rebuild them. Also you don't compile .java files into .java files they become .class files. You can write an ANT build script to handle the heavy work for you.

Categories

Resources