improve.dk
Just another mindless drone looking for the perfect stack
posts - 227, comments - 489

Compiling Java in Visual Studio

Written on September 29, 2007 by Mark S. Rasmussen in Development: Visual Studio

I often see my fellow comp. sci. students writing their (relatively) simple Java code in applications like Emacs, Nano or Eclipse. I'm not fond of either application. I much prefer Visual Studios text handling, solution overview, output windows and so forth. What most people don't know is that you can actually extend Visual Studio to a great extent. One way to extend Visual Studio is to write plugins using .NET, but there's a way that is much simpler (albeit also more limited). I will now show how you can make Visual Studio compile and run your Java applications all within Visual Studio itself.

First of all, you need to download and install the Java JDK. Basically, you need to be able to call "javac" and "java" from any command prompt - which means you have to setup the environment settings so you have your JDK bin in the PATH variable.

javac_1

javac_2

Create a new Visual Studio project. It really doesn't matter much what type you choose as there is no native Java project types. Choosing J# will not give you any advantages over, say a C# project. In this example I'll use a C# Class Library project.

javac_3

Start out by deleting the automatically created Class1.cs file. Add a new text file instead, I'll call it MyApplication.java. You can write any standard Java code in the Java files, just like you'd ordinarily do.

javac_4

One of the really cool features of Visual Studio is that it actually includes Intellisense for a lot of the standard Java classes, so you're not left totally on your own.

javac_5

Now comes the compilation part. Add a new text file to the project and call it Compile.bat. This will be the bat file that manages the actual compilation and execution of the application afterwards. Leave the file empty for now, we'll enter the code in a short while.

javac_6

Go to Tools -> External Tools...

javac_7

Add a new entry called "Javac", set the command path to your Compile.bat file and make sure the directory is set to the ProjectDir macro path. Check the "Use Output window" checkbox, this ensures the output is output directly into the Visual Studio output window.

javac_8

Now enter the following into the Compile.bat file:

del Output /S /Q
mkdir Output
javac *.java -d Output
cd Output
start java MyApplication

Modify the MyApplication.java file so it ends with a call to System.in.read(), this ensures the application will stay open after we start it.

import java.io.*;

class MyApplication
{
	public static void main(String args[]) throws IOException
	{
		System.out.println("Hello World!");
		System.in.read();
	}
}

Now simply go to Tools -> Javac and watch your Java application compile and run.

javac_9

You can of course modify the build script in whatever way you wish to support larger applications. You could also use ANT build scripts, unit tests and so forth. To make compiling easier, you can create a key command (Tools -> Options -> Keyboard -> Tools.ExternalCommandX where X is the Javac commands index in the Tools menu) to the Javac command in the Visual Studio settings, I use Ctrl+Shift+J for Java compilation myself.

Feedback

Gravatar

Adams Walthwind wrote on 9/29/2007 11:26 AM

How do you debug Java using Visual Studio? Without the ability to debug... well... it's useless!
Gravatar

Mark S. Rasmussen wrote on 9/29/2007 1:33 PM

I do not know how to debug Java applications through Visual Studio, I shall not say if it's impossible though.

This article is not meant as a universal replacement of IDEs like Eclipse, it's meant as a replacement for Eclipse, Emacs, Nano and so forth for smaller projects where debugging is not necessarily necessary (as was said on the most recent JAOO, if you ever need to debug, it's just because you neglected on writing tests ;)).

I don't like using neither Emacs/Nano/Eclipse, especially not since it usually means I will have to run it over a terminal on the university campus. If I can just convince my fellow students to use Visual Studio for suited projects, we can easily develop in our comfortable Windows environments, using source control and everything else that we're used to, from our normal Visual Studio environment.
Gravatar

Srinivas wrote on 12/19/2007 5:34 AM

Thanks, really very easy.
Gravatar

Sam wrote on 6/3/2008 1:08 PM

Hi there i can not fallow you can you describe these steps in a movie.

Regards
Sam
Gravatar

Mark S. Rasmussen wrote on 6/3/2008 11:49 PM

Hi Sam,

What parts are you having troubles with? I'll see if I can explain it better then.

Mark
Gravatar

Ivan wrote on 6/5/2008 12:31 AM

Hi, Mark,
thanks for the walkthrough.
I'm not sure how tests would alleviate the need to step through the code and see what happens to variables. About writing tests, tests show if there is a problem, but don't pin point where in the code it is. Do you agree?
Ivan
Gravatar

Mark S. Rasmussen wrote on 6/5/2008 11:29 AM

Hi Ivan,

Don't get me wrong, I wouldn't be able to do any "normal" piece of work without my trusty debugger (I haven't done any larger development in Java, only .NET / Visual Studio).

As for the tests, I somewhat agree, and somewhat disagree. From a purely theoretical point of view, if you've partitioned your classes and functions to an atomic level, each function doing nothing but one action - having a test of said action fail, would pinpoint the exact location of the problem.

Naturally we can't partition our functionality this much, so yes, tests do not show the specific line number where the error occurs - and that's not what they're for either. The JAOO quote is a provocative one that is meant to fire up discussions. While I do agree with the quote from the theoretical standpoint, I haven't ever worked on a project with a level of testing high enough to make it true.

Mark
Gravatar

Tony O wrote on 12/5/2008 4:32 PM

I've achieved something similar using post-build events. It gives you the benefit of building projects separately.

Post-build command line:
<pre>$(SolutionDir)compile-java-app.bat $(ProjectDir) "..\lib\classes\." foo.jar</pre>

compile-java-app.bat takes the given command line arguments and compiles the java code and creates jar file.

syntax:
<pre>compile-java-app.bat projectDirectory classpath outputJar</pre>
Gravatar

Lennart wrote on 4/25/2009 11:04 PM

You said:
>I don't like using neither Emacs/Nano/Eclipse, especially not since it usually means I will have to run it over a terminal on the university campus. If I can just convince my fellow students to use Visual Studio for suited projects, we can easily develop in our comfortable Windows environments, using source control and everything else that we're used to, from our normal Visual Studio environment.

I say:
Eclipse is exactly the same as visual studio. It has comparable features in most regards, especially in the fields that you mentioned: it runs in windows, has source control and it does not run in a terminal.

In fact, I would go as far as to say Eclipse surpases VS in manye regards.

It's free, it runs on many platforms, not just windows, it has support for more languages and environments than any other IDE that I know of. It has an amazingly large support base.

The editor itself is really good, especially for java / C++ development.

:) There :D I just thought your students shouldn't only hear your version ;)
Gravatar

Mosh Jahan wrote on 4/27/2009 9:10 AM

I disagree. Visual Studio 2008 is way better than Eclipse in terms of features and especially in terms of performance, the editor is MUCH faster responding to user events like intellisense.
Gravatar

HC wrote on 7/17/2009 10:17 AM

What's this?? The only reason to compile Java using VS is the production of IL code from Java. To do this you can use Eclipse, and it's free.
Gravatar

Erik wrote on 8/26/2009 7:37 PM

Hello!

Intellisense don't seem to work for me, any ideas?
Gravatar

Neil wrote on 10/8/2009 2:34 AM

Does this work in Visual Studio 2008 or only in Visual Studio 2005? Is there anyway to get it to work in 2008? I tried these steps in 2008 and when I try to run it, the CMD window pops up with some stuff written in it, but closes right away. Not sure exactly what the deal is.
Gravatar

djaus wrote on 5/6/2010 7:09 AM

I modified (improved) the batch file:
(I placed the build batch file in teh project directory)
Batch File:
===========
rd Output /S /Q
mkdir Output
set path=%path%;"C:\Program Files\Java\jdk1.6.0_20\bin"
javac *.java -d Output
echo %2
if NOT %2c==c goto end
cd Output
if exist %1.class (start java %1 ) else echo %1.class not built
cd ..
:end
==================

To use it you need one or two arguments, separated by a space.
The first is the name of the class to run.
The second is optional and can be anything.
If the second arguement exists (not blank) then the run is skipped.

In the Tools--External Tools .. Javac dialog,
enter the arguments and/or click on prompt for Arguments
eg 1 Program x will skip the run
eg 2 Program will run Program.class

I do find it hard to believe that no one has configured an editor with Intellisense etc for Visual Studio.

ToDo: Create a Java Project for VS and set its build events to call the Java build
Gravatar

t0meck wrote on 7/9/2010 12:21 AM

VS. Yes. Maybe it is faster but hey! Eclipse provides a lot of plugins and also different type of editors. You can add an editor which may be faster than the one included in VS.

I'm not saying VS is bad. But I don't like the idea of being trapped in single system. If I develop an application in VS it is compiled to run only on windows and since windowses aren't free i don't like them. I must say I'm encoraging anyone to use different systems and if Your students want to do their work using something different than VS which comes from M$ then I agree with them.

Besides eclipse is using native system controls from SWT library and not being so overloaded with graphics and everything which is packed into VS it works faster. The only disadvantage might me the fact it is written in java which is slower due to fact the code must be fully compiled from bytecode to machinecode but being written in java is also its biggest advantage of being portable to other systems. Of course if it was written in python it would be lightning fast but authors would have to write different versions for each platform and then there would be the problem with plugins but maybe in future it would be "converted"

And lastly. 7GB for VS vs. ~150-500mb of Eclipse IDE.
Gravatar

Shimmy wrote on 7/13/2010 5:57 PM

Will this work in VS 2010 as well??
Gravatar

Lowery wrote on 7/27/2010 5:33 PM

I don't think the intellisense would work in VS 2010. I suspect the original author used VS 2005 for the post. My understanding is that support for the Java-esque 'J#' language was removed as of VS 2008 (lawsuits and all :( ) and so the Java package/language intellisense is not there.
Visual Studio can be extended to support new languages however though I haven't seen a project that is already doing this for Java.
Gravatar

Thomas wrote on 6/12/2011 5:31 PM

Hello, i came quite far =)
Only i came up with this error.
Any idea?


C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1>del Output /S /Qmkdir Outputjavac *.java -d Outputcd Outputstart java MyApplication
Parameter format not correct - "Qmkdir".

Gravatar

Thomas wrote on 6/12/2011 5:36 PM

My fault, after reading the .bat again, i saw there should be a space after /S /Q, although i came on another 2 errors. Anyone know?

-----


C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1>del Output /S /Q

C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1>mkdir Output
A subdirectory or file Output already exists.

C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1>javac *.java -d Output
MyApplication.java:1: illegal character: \187
import java.io.*;
^
MyApplication.java:1: illegal character: \191
import java.io.*;
^
2 errors

C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1>cd Output

C:\Users\Thomas\Documents\Visual Studio 2008\Projects\ClassLibrary1\ClassLibrary1\Output>start java MyApplication

--
Gravatar

Mark S. Rasmussen wrote on 6/13/2011 11:19 PM

@Thomas
This seems to be a problem with the unicode byte order mark. Try saving the file explicitly as UTF8, Unicode or ASCII and see if not you get them working.
Gravatar

Thomas wrote on 6/14/2011 6:06 PM

Hey, this seems to work. I found out its required to safe the MyApplication.java in the same encoding as the Compiler.bat
Knowing this i finally succeed in building the Java Program :) Thank you very much!
Gravatar

jake wrote on 6/17/2011 9:36 PM

Hi i dont know what

Compile.bat file and make sure the directory is set to the ProjectDir macro path

mean

what do i have to put on commnad:
Gravatar

jake wrote on 6/18/2011 2:19 AM

i created java file and try to type

but did not show any class for java and no colors

and error said "illegal character" stuff

i dont understand
Gravatar

Mark S. Rasmussen wrote on 6/18/2011 9:45 PM

@Jake
Look at the screenshots and it should be clear where the ProjectDir macro needs to go.

Note that this post was written in 2007 and referred to Visual Studio 2005 - this probably doesn't work the same in either Visual Studio 2008 or 2010.
Gravatar

Valentino wrote on 8/6/2011 7:13 PM

Hi Mark, can you please explain in more details the following:

"Basically, you need to be able to call "javac" and "java" from any command prompt - which means you have to setup the environment settings so you have your JDK bin in the PATH variable. "

I included the jdk bin to my project directories include folder.

thanks
Gravatar

Mark S. Rasmussen wrote on 8/6/2011 10:59 PM

@Valentino
See this thread for more info on how to setup the environment variable:

superuser.com/.../javac-command-not-found
Gravatar

Anthony of Sydney Australia wrote on 11/21/2011 11:09 PM

Question please,
If one can compile java within the MS Visual Studio, can an executable version of the java program be achieved? Thank you.
Gravatar

Ali wrote on 3/29/2012 8:25 PM

At the final step when I was trying "tools->javac" it wants an argument can you please tell me what is it ????

Post Comment

Name  
Email
Url
Comment
Please add 1 and 3 and type the answer here: