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
Related
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
So I'm trying to run
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_73
javac com/darkblade12/enchantplus/*.java
jar cvf program.jar -C com/darkblade12/enchantplus
in command prompt as I'm not fluent in any language or Eclipse, but I get a cannot find symbols error for many of the items "compiled", I originally just tried to recompile a .jar in Java7 rather than Java8 because I had the source code and needed it in the format for Java7, so any help with either would be great.
Your question is not very clear, but, I guess you only want to compile your classes.
The value of JAVA_HOME variable has blanks in it. You might want to put it in quotes or use the windows shortened path names, like shown here How can I find the short path of a Windows directory/file?.
You are missing a source file, or possibly an external library your program depends on. If you find Configuration.java, you can add it to your enchantplus folder, maker sure to give it the same package name as the one in IndependantConfigurationSection.java and it should compile. If it is a .class file packaged in a .jar file,you need to add that jar to your classpath using
javac -cp {path-to-class-or-jar} com/darkblade12/enchantplus/*.java
I'm trying to cmd line compile a .java selenium test script into a class file that I can run from the command line.
All of my selenium jar files and all other supporting jar and lib files are in C:\JarFiles
My CLASSPATH is set to C:\WDJarFiles*
I am working at the command line here: C:\EclipseIDEworkspace\MC3\src\Tasks
My .class files are located here C:\EclipseIDEworkspace\MC3\bin\Tasks and I'd like to be able to update them at that location.
My folder structure was set up by using Eclipse IDE so I'd like to keep the existing folder structure but now I want to be able to compile my .java files from the command line and update the .class files.
So, when I run javac like this:
javac Edit.java
It compiles OK and the .class file gets created in the same folder where I am running the javac command -- but -- I also get a huge number of other .class files in this same directory! These look like supporting class files.
I'm not sure what my cmd line javac syntax should be to:
Compile my .java file so its .class file gets updated in the C:\EclipseIDEworkspace\MC3\bin\Tasks folder.
I don't get all those other .class files created in my working folder C:\EclipseIDEworkspace\MC3\src\Tasks
Thanks for any help...
You should try the '-d' option to specify an output directory:
javac Edit.java -d ..\..\bin\Tasks
About the multiple other .class files, you probably have many nested classes into your Edit.java file?
I have exported a jar file that I want to run the console. The code compiles and runs correctly in eclipse but I am having an issue running it from the console.
To me it looks like the referenced jar's I added via built path in the Eclipse project file and not being added to the export. If that is the case, how do I ensure that they do? If not, what am I doing wrong?
When you export your source code's class files to a jar using eclipse, only the .class files of your source are exported! Hence your exported jar file doesn't contain the referenced jars you mentioned in eclipse! Due to this, the error occurs while executing from command prompt.
Solution:
Take all the jar files required to execute the program, store it in the same directory as you store the exported jar file. Now while executing the java command, provide all the jar file's names in classpath field as following:
java -classpath .;JAR1.jar;JAR2.jar MainClass
Once you do this, your problem should be resolved!
The dependencies need to be on the classpath, i.e. run like this:
java -cp <path_to_jar1>;<path_to_jar2> -jar ScrumTimeCaptureMaintenence.jar
When running from the command line make sure any dependencies are set on the class path by listing them in the -classpath parameter
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