Compile Java source to another directory - java

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.

Related

Compile java class in VSCode

I'm writing a code to run the original coded program from my professor through the class files. So basically I downloaded my coded program of my professor to start coding a class file in the visual studio code. But when I run, it could not find my class file which means that I haven't compiled it, then I have searched up some ways to compile the file which has the javac or some like that and it turned out:
"'javac' is not recognized as an internal or external command
operable program or batch file.
The system cannot find the path specified."
So how can I easily compile the class file and set the classpath?
Visual Studio Code is just a code editor. VS Code can't actually compile your code: Only help you write it. Various extentions to VS Code can automate the process of running the compiler, but you still need to install the compiler yourself.
To compile your code you'll need to install the Java Compiler (javac) which is a separate program. You can install the Java Compiler by installing the Java Development Kit, which is a set of tool for developing Java programs.
Typically the Java Develoment Kit (JDK) instillation will set up your operating systems paths so javac will refer to a valid program after your restart VS Code and your consoles. If it still doesn't, refer to the answer linked my MikaelF's answer. You'll need to manually tell your computer where you installed the JDK.
only jdk can help you to compile java class;
open cmd type:"java -version"; if not have proper response(the version of jdk),you should download jdk from oracle and install it,and ofcourse add the bin directory like this(D:\workSoft\JDK8\bin) to the path environment;
if you had install the jdk correctly then visual studio code will compile the .java file to .class file automatically

Compile vtd-xml linux?

How should one compile vtd-xml for java on a linux box? I downloaded the project from sourceforge, but it only comes with a .bat script. I set classpath to reference the jar file, but I get all sorts of symbol not found errors when I try to run my compile my own script. I think vtd-xml needs to be compiled as a project before I can use it in other scripts, but I don't know how. I'm new to java...
you should not. Go to https://sourceforge.net/projects/vtd-xml/files/vtd-xml/ximpleware_2.12/VTD-XML%20Standard%20Edition/ximpleware-2.12-java.zip/download
And download the zip, it has been verified to work...
Just put vtd-xml_2-12.jar in your classpath
Disclaimer: I am the author of vtd-xml.

Javac simple package (JavaBib)

Dear Java Programmers,
JavaBib (http://code.google.com/p/javabib/) is a simple open source java package for which I would like to generate bytecode.
The source can also be obtained by version control via
svn checkout http://javabib.googlecode.com/svn/trunk/ javabib-read-only
Although it consists of only a few java files, I am not able to compile it using
javac *.java
I get
cannot find symbol
error messages for nearly all of the files.
The source files of the dom package all reside in one folder. Therefore, I think it should not be a problem with the classpath. I have tried different values for it nonetheless.
There are additional source files in other directories. I tried copying them all in one directory and run javac in this directory, but javac was complaining about the same imports.
Unfortunately, I have hardly any experience with java and can't fix the problem myself.
Therefore, I would be grateful for hints towards additional diagnostics in order to pin down the problem.
I use Ubuntu 12.04 as operating system and haven't set any classpath or sourcepath variable. At least javac seems to find imports like "java.util.list".
Thanks in advance for your help.

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.

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