How to convert a .java file to a .class file? - java

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

Related

run java program created in intellij from command line

I have a simple Java Maven program that I created in Intellij. It has Two classes Main and Read. I was able to build a jar and run it. However if I zip the source code into a folder , would I be able to compile it using command line? Something like javac ? How should I do this? Shall I run commands in Java folder in the project? Many thanks.
As far as the Java class loader is concerned, a .jar or .zip file is the same as a directory containing the the files, and .jar and .zip files are generally used to distribute compiled Java packages. Here you can read more about it.
For compiling the zip file: use the -classpath option to javac and java. We could, for example:
javac -classpath .:/users/johnr/java:/opt/jdk1.1.6/lib/classes.zip Hello.java

Compiling multiple .jar files with .java class

I currently have a java class which I am attempting to compile using Linux terminal along with a folder (/libs) which contains about 9 .jar folders.
I am struggling trying to attempt this.
I am aware of how to compile a single library to a class:
javac -classpath /libs/example.jar MyMainClass.java
just unsure of how this can apply to multiple libraries.
Thanks.
You can either refer to an entire folder containing all the libraries such as javac -cp lib\* MyMainClass.java
Alternatively, you can refer individual libraries as such: javac -cp lib1\Library.jar;lib2\Library.jar MyMainClass.java (separate with semicolon)

Compiling four java files within one package using javac

I have four java files in my folder. They are all in the same package. Here's the package declaration
package com.osama.GHide
All of these classes are in the same package. I want to know how can I compile them using javac (i mean i do not know how to compile multiple files that are using each other). And once that is done how do I launch then using java command in the CLI? here are the file names.
EnteringPoint.java
HidingProcess.java
ListFiles.java
From the project's root directory:
javac src/com/osama/GHide/*.java
To run, assuming no other dependencies:
java -cp ./src com.osama.GHide.EnteringPoint
(Assuming EnteringPoint has the normal main function.)
The javac command compiles all the .java files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class files in the same directory, which may or may not be what you want.
To put them in a different directory, use the -d option and supply a path.
javac -d bin src/com/osama/GHide/*.java
Then to run:
java -cp ./bin com.osama.GHide.EnteringPoint
You can run by using command :- javac -cp src/com/**/*.java
I am asuming there is no other dependenices and your root class name would be src only.

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.

how to convert .java file to a .class file

Can anyone tell me how I can convert a .java file into .class file with an executable bytecode?
A .java file is the code file.
A .class file is the compiled file.
It's not exactly "conversion" - it's compilation. Suppose your file was called "herb.java", you would write (in the command prompt):
javac herb.java
It will create a herb.class file in the current folder.
It is "executable" only if it contains a static void main(String[]) method inside it. If it does, you can execute it by running (again, command prompt:)
java herb
From the command line, run
javac ClassName.java
I would suggest you read the appropriate sections in The Java Tutorial from Sun:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
Use the javac program that comes with the JDK (visit java.sun.com if you don't have it). The installer does not automatically add javac to your system path, so you'll need to add the bin directory of the installed path to your system path before you can use it easily.
To get a .class file you have to compile the .java file.
The command for this is javac. The manual for this is found here (Windows)
In short:
javac File.java
As the others stated : it's by compiling. You can use the javac command, but I'f you're new at java, I suggest you use a software for compiling your code (and IDE) such as Eclipse and it will do the job for you.
You can not compile any java file to class directly.
It should have main method to compile it in class file
After compiling it you can jar it.
java -jar AppName.jar
http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

Categories

Resources