Sam Hobbs

Sam Hobbs

  • 51
  • 29.3k
  • 2.1m

Java in Visual Studio

Dec 30 2011 10:36 PM
This is a Java question; actually it is more of a blog.

I have searched for ways to build Java files in Visual Studio. I found a few tools that are not free. I also found a web page with a bat file and relatively complicated instructions. I want something that can be somewhat automatic. I know that it is possible to create build rules in VS and it often takes time to figure ou how to do that. I have done it in the past but the VS 2010 way of doing it is different from the past. I have managed to get a start on doing it. If interested, you can try doing the following and hopefully someone can add some fmore to this. First see Specifying Custom Build Tools.

Note that I am using VS 2010; you can do something similar with VS 2008 but I don't know the details of how. Create a new project. In the Installed Templates on the left, expand the C++ node and select General and then in the right side select Empty Project. The reason I say C++ is because it will allow custom build rules but apparently not all language projects do; C# projects do not.

Then add a Java file to the project; a file with a java extension. You can create a text file but give it a java extension. Here is one important detail: save it as a non-Unicode file. I saved the file with encoding and chose US-ASCII encoding. I hope you are able to figure out what I mean. It is probably possible to use the Java compiler with a Unicode source file; I will try to figure that out later or perhaps someone can contribute that here.

Then in the Solution Explorer right-click on the project and choose Properties. First change the Configuration to All. Then in the left side expand Configuration Properties if not expanded and then select General, although I think General is the default. Then in the Solution Explorer click on the Java file. Yes, it is possible to do that; try it. The Property Pages page will change. In the Item Type select Custom Build Rule. Okay, you are almost there. Next click on Custom Build Rule on the left. For the Command line I have:

"C:\Program Files\Java\jdk1.6.0_21\bin\javac.exe" -verbose "%(FullPath)"


The verbose option is optional. For Outputs I have:

$(ProjectDir)%(Filename).class

Note however that the class in the Java file is the same as the filename; if those are different, then this rule will not work and the compiler will be executed for every build even if it is not needed.

I do not know how to build an executable from Java source code; perhaps someone can help with that. I think we would then need another build rule for linking or whatever. Is an executable Java file a jar file?

So if anyone wants to play with that, then you can let us know if you have anything to add.

Answers (3)