How to compile Java code without an IDE? - java

I am a beginning programmer that has been using an IDE to learn to code. However I want to try using a text editor to see which one I would like more. My question is, once you download a text editor, what tools do you need to compile and run a program and how do you set them up?

You just need the JDK. Installing it (it has an installer) should set it up sufficiently.
When you want to compile something, use the javac tool:
> javac MyClass.java
When you want to run something, most likely you'll want the java tool (although of course if you're doing servlets or such, you'd use something else):
> java MyClass
Or of course, go ahead and use a separate text editor if you like but continue to use the IDE to compile, not least because presumably any IDE you were using had an integrated debugger, and a debugger is an essential tool.

If you want to use a text editor for java programming don't forget to add java to your Environment Variables Path. You can check if java is already added to your path by running java -version in your command line. If you get nothing you have to add C:\Program Files\Java\jdk1.8.0; to your path (your version of the jdk may be different, change it to the name you have). Don’t forget that semi-colon ; at the end.
Now you are set and ready to compile and run java programs just as explained by #T.J. Crowder in the answer above.

Related

Just installed vim-javacomplete2 but doesn autocomplete at all

I am newbie to plugins in vim. I am using vim for a year but without any experience with plugins. So i installed Vim-plug, then add Plug vim-javacomplete2' to my .vimrc and :PlugInstall it. It was installed. Then I add autocmd FileType java setlocal omnifunc=javacomplete#Complete to my .vimrc as well - as is in github. But then, ther is nothing else. So some commands works. I Tried for example JCgenerateAccessors - which generate getter and setter upon my private fields. So commands works. But The autocompletation doesn (such main function). I am making classes within a file in my directory. Then javac Test.java and java Test - which runs it. But still, when editting in vim the Test.java, no autocompletation. What I did wrong?
Might a a bit late
<C-x><C-o> (ctrl + x,ctrl + o) will bring up a list of options.
Press this in insert mode wherever you would press ctrl + space in other IDEs
i.e.: after System.
Expanding upon #iaquobe's answer, autocompletion while typing or more specifically after the dot notation, isn't enabled by default with this plugin, unless triggered manually using C-x,C-o, which can -and will- become tedious over time.
There're other autocompletion plugins that can be installed along with other completion engines, and be configured to integrate with them, namely: vim-mucomplet, youcompleteme, and neocomplete.vim.

Why does one IDE not let me add parameters for JComboBox?

I have two Java IDEs that I alternate between. jGRASP is for simpler tests and miscellaneous files, and Netbeans I use for more complex projects.
I'm currently using jGRASP and I'm trying to declare a JComboBox<String> object, but when I try to compile it, it gives me the message:
type javax.swing.JComboBox does not take parameters
But when I use JComboBox in Netbeans and set its parameter type to String, it doesn't give me that error. Does anyone know why this is?
Support for generics was only added to JComboBox in Java 1.7. Your jGRASP IDE is probably using an earlier version of Java
You can turn on "Settings" > "Verbose Messages" in jGRASP to see which "javac" is being called during compilation. Look for the "actual command sent" line in the output.
On Windows, jGRASP will look for the newest version of Java when it starts and use that to run itself and run/compile your programs. If the Java part of the registry is futzed up, this may fail. You can use "Settings" > "jGRASP Startup Settings" to manually select a Java installation. If you do that, just remember to change it when you upgrade Java (probably just change it back to [default]).

Compiling Java file with code from within a Java file

I'm currently creating a personal (maybe public) java terminal. I want to create a command that will create + compile a Java file on execution, except I'm not too sure on how to actually do this. Is it possible? Or am I just dreaming?
You could also use Groovy - it's quite handy if you just want to compile and run a line or two of Java code from within your application. The application may be in regular Java, with Groovy used only for compilation of the dynamically generated code. Whichever solution you choose, be careful, as executing user input as code can lead to security issues (vulnerability to injection attacks).
compile a Java file
See the STBC. It uses the JavaCompiler to compile the code in the text area.
I agree with #eee's comment that javax.script is probably a very nice fit for your project, script code is easier to deal with than Java code. I've successfully used it in the past for a plugin API, I don't remember having had any problems to get it up and running.
Most projects that I know of that compile real Java at runtime use the Eclipse compiler to do so. The Java 6 javac can be accessed completely programmatically as well. I've never used either of these myself. These two and some other compilers can be accessed via Commons-JCI if desired.

Can't read AppletViewer properties file - Applet

I created a Java applet program and compiled it. As a result of the compilation, it generated a class file. But, it showed the following warning message while running the applet with the command prompt using the appletviewer command.
D:\Applets\Applets>appletviewer FirstApplet.java
Warning: Can't read AppletViewer properties file: C:eswaran_s.VMSPL\.hotjava\properties Using defaults.
When I run this command once again, it does not show the warning message, but the applet is not running.
How can I solve this issue?
My solution to this problem was to simply (in Eclipse) go to Run-> Run configurations... and open the JRE card where I set Java SE to a newer installed version. (in my case from SE 6 to SE 7)
The file .appletviewer should be in your home folder. If it's not exist then default file is created. You can also place it in the %USERPROFILE%\.hotjava\properties.
If you run appletviewer it runs because you got any message from it. Suppose your instance is created on the %PATH%. But to run properly you should supply the argument text file that contains deprecated <APPLET> tag. It doesn't matter what extension it has.
Your extension is wrong. You should pass HTML file path like this:
D:\Applets\Applets>appletviewer htmlfile_name.html
Instead of this:
D:\Applets\Applets>appletviewer FirstApplet.java
It is often very difficult to write, compile and run applets in the applet viewer from the command prompt. A better way would be to download the program Eclipse from the Internet. I often got lots of errors when trying to run programs from the command prompt, and Eclipse automaticly compiles, emphasises special words (like new, break, byte, short etc.) and even error checks your code (it underlines errors with a squiggly red line like Microsoft word's spellcheck). All my problems have been solved! I seriously recommend downloading it.
i think your jdk miss some package or loss path so re-installed jdk latest version and run your program. and check your htm program is correct or not may be some time occurred due to wrong htm program.
I had the same problem following the instructions for a similar tutorial from Oracle's Java: A Beginner's Guide - Sixth Edition (pg 515). The directions tell you to immediately run the .java file with the applet viewer. However, you must first compile it. So, C:>javac FirstApplet.java. This will compile the program and create your FirstApplet class file, which is what is currently missing from your execution. Then, C:>appletviewer FirstApplet.java. Your applet should run fine. Seems odd that the tutorial would leave out this instruction.
Shoutout to RAB teaching broken code (Applets have been deprecated in most browsers)
First, make sure you have the javadoc folder (simply labelled "doc" once extracted) inside your jdk folder as advised here:
http://www.jcreator.com/installation.htm
You can download the doc file from here for Java SE 8:
http://www.oracle.com/technetwork/java/javase/documentation/jdk8-doc-downloads-2133158.html
Make sure you build the project first before running it (Menu/Build/Build Project).
Now run your project.
if you are using command line to run the file, use this particular line after importing packages.
/*<applet code = "Class_name.class" width=300 height=250></applet>*/
I have change this line in the prompt and ready:
D:\Applets\Applets>appletviewer FirstApplet.java

How can I compile and run a Java program?

How can I compile and run a Java program?
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#win32-2b
Is English difficult for you? There are tutorials for your language too.
to compile:
javac path/to/file/with/main.java
then to run:
java path/to/file/with/main
I would suggest that if you are starting with the java language, first use an IDE (Eclipse, Netbeans, Intellij IDEA or another. For a first step let the IDE do his job. Then once you evolve quickly use a build manager (Ant, Maven, Gradle, ...). It is a good practice to always have a build manager in place to keep the build in control.
To compile (assuming you are in the same folder as the file is)
javac "filename.java"
To run
java "filename"
make sure you have set the environment variables correctly, so that the command line can detect where javac is.

Categories

Resources