• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Compiling Java Files/Folder

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone !

I am new to programming. I'm trying to learn Java SE 7. But often I find myself struggling with real life problems, such as the compiling of my open sourcecode; often downloaded from the Internet.

My question is: How do I compile these sets of Java files that are spread across various folders ? Do I first convert to Jar files ? I'm confused.

- Thanks
 
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Fred,

So in order for you to compile source code, I suggest you download a java developers kit often abbreviated as JDK. It can be found here http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html.

After you have downloaded this, assuming you are on windows, you must go into Computer>System Properties>Advanced system settings>Environmental variables...> and there you must set your PATH variable to the bin folder of the JDK you have downloaded. (This is because in order to compile and run your code, you require the commands java and javac, which are not implemented into windows, therefore when you input those commands into the command prompt, the OS doesn't know where to find them unless you specify where to find them.)

Your PATH variable should look something like "C:\Program Files\Java\jdk1.7.0_40\bin" without the quotation marks. This is mine, yours might be different. Also it is a good idea to create a CLASSPATH variable with the variable value of "." again without the quotations.

Once this is done, you can write your source code on whichever application you desire, but one crucial part is to save it as a .java file.

If you wish to compile and run your programs via command prompt, then open up command prompt by searching for "cmd" in the start menu. Once in the command prompt interface you must change the directory to wherever your file is saved. For instance, say i save my file to C:\Users\Tom\Documents\JavaPrograms. By default, you will be in the C:\Users\Tom directory, in order to get to the directory listed above you will simply type the following command "cd Documents\JavaPrograms". No quotations.

When in the correct directory it is time to compile and run our code. Say we have a file called HelloWorld.java (Very original....), to compile this we write "javac HelloWorld.java". javac being the command for java compiler. If the code has no errors, then this will compile and write a new file in the same directory called HelloWorld.class, this file is in binary and you will not be able to understand it or edit it (Binary consisting of 1's and 0's.) After compiling, we would like to run our program, therefore we simply write the command "java HelloWorld" notice how we do not include the ".java" this time after HelloWorld. The output of this program is to display the string "Hello, World!" on the console. Therefore after typing "java HelloWorld", we expect the next line to say Hello, World!.

Hope this helped, if problems persist just post it here.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! I'm actually on a Mac - OS X Mountain Lion. The latest upgraded version. I'm pretty hip to the JRE and JDK. I have those installed. Can you give me the instructions for Mac ? If you wish to email directly <pina.frederick@me.com>, that probably would help more.

Thank you !
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, the JRE and JDK will serve you well. Anyways you can find the instructions for Mac listed here... http://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, for mac the same instructions apply after you have set your variables with the exception that you will be using terminal instead of command prompt. For file management and quick compile and run time, I suggest eclipse. Although it is good to know how to compile and run code manually via terminal.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I quickly glanced through those linked pages. The information is a bit dense. Also, I'm simply looking to compile my source code and actually see it in action. I'm not trying to relearn all sorts of material. I thought there some simple instruction that I could follow. The downloaded folder produced a folder filled with a few more folders and a series of Java files. Again, the question is how do I compile this source code ?

Screen-Shot-2013-09-24-at-5.14.48-PM.png
[Thumbnail for Screen-Shot-2013-09-24-at-5.14.48-PM.png]
Image
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the most simplest form;
Open terminal > go to the correct directory > javac yourFile.java > java yourFile.

Unless paths are set you will receive an error message.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you can download an IDE (Such as Eclipse). Where your code is compiled and ran inside Eclipse. Meaning you won't have to worry about setting paths and such.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what confuses me. Your instructions are for a single Java file, when the source code is a folder with a list of many Java files. Aren't those files parts of the game ? How can I compile an individual file and still get the game to run ? That doesn't make sense.
 
John Vorona
Ranch Hand
Posts: 48
Mac OS X Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java programs consist of many classes, but it MUST have a single class with a main method.
All of those files which are part of the game consist of code with absolutely no output. Just constructors, variables, and methods which go into the class with the main method. All of those files need to be compiled via javac yourFile.java command. But most of them will give you no output so if it is compiled correctly there is no use running it via java yourFile. The file you have to worry about is the file which has the class with the main method.
Which starts of like this

This file requires all of those other files in order to run properly (essentially, the files which do not contain a main method are building blocks). Your question was how to compile and run a file. This is beyond that, if you are confused about how programs work, I suggest you create another thread where you can get a more thorough explanation.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I understand what you're telling me. But, how do I figure out how to run my source code. I downloaded the source code, and I got a folder in my desktop; with 18 different folders, all filled with Java files.

How do I compile this source code ?

ch01src
ThreadPool.java
ThreadPoolTest.java
__________________

ch02src
Animation.java
AnimationTest1.java
AnimationTest2.java
build.xml
FullScreenTest.java
images
ImageSpeedTest.java
ImageSpeedTest2.java
ImageTest.java
ScreenManager.java
SimpleScreenManager.java
Sprite.java
SpriteTest1.java
SpriteTest2.java
_________________

ch03src
build.xml
images
src
________________

ch04src
build.xml
images
sounds
src
______________

ch05src
build.xml
images
maps
sounds
src
______________

ch06src
bin
build.xml
lib
README
src
_____________

ch07src
build.xml
images
src
______________

ch08src
build.xml
images
src

______________

ch09src
build.xml
images
src
___________

ch10src
build.xml
images
src
___________

ch11src
build.xml
images
src
_____________

ch12src
build.xml
images
src
_______________

ch13src
build.xml
images
src
______________

ch14src
build.xml
images
lib
scripts
src
______________

ch15src
build.xml
src
ch16src
build.xml
images
src
______________

ch17src
build.xml
fonts
src
____________

ch18src
build.xml
icon.gif
images
maps
res
sounds
splash.gif
src
tilegame.jnlp
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you start having more than a couple of .java files in a project you need to start looking at build tools such as ant or maven.
I notice that there is a build.xml in each of you project folders - if I remember rightly that is the configuration file that ant uses.
Of course there may be other build tools that use a build.xml file as well. Does the site you downloaded the files from or the book they accompany* not give you any instructions about how to build the projects ?



*I'm assuming as each folder is in a project called chXXsrc, these are sample codes from a book
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you are looking at contains build.xml files. These files contain information on how to compile the program, and are used for a tool named Apache Ant.

Ant is a build tool: it is used to make it easier to compile complete projects, with multiple source files, libraries etc.

Once you've installed Ant, it should be very simple to compile the project: in a command prompt window, go to the directory that contains build.xml (with the "cd" command) and then simply run: ant
 
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

My not‑at‑all humble opinion:-
Stop downloading code like that. You are probably downloading the examples for book chapters. You will learn nothing from downloaded code. You learn by writing code. You can copy the code by hand, or write your own. Then you can forget all about package names, ANT files and all that sort of thing.
Consider ANT files, package names, etc, as advanced features which you can learn after you have learnt basic compiling.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! I don't know how to update Ant via Terminal ?

Last login: Wed Sep 25 19:28:34 on ttys000
Fredericks-MacBook-Pro:~ frederickpina$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
Fredericks-MacBook-Pro:~ frederickpina$

Should I update this before I attempt to run the open course code ?






 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone !

This is where I found that Java open-source code online.

http://www.brackeen.com/javagamebook/#download

File Name: allsrc.zip (4.3MB)

Can anyone show me how exactly to compile this on an OS X Mountain Lion.

Thanks !

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have Ant.

1. Unzip the file, then cd to for example the directory ch02src.
2. Simply enter the command: ant

Ant will now build the code, and after a while you see BUILD SUCCESSFUL

Run the example program, for example: java SpriteTest2
 
Campbell Ritchie
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote: . . . Run the example program, for example: java SpriteTest2

Don't you need to quote the fully‑qualified name of the class?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jesper de Jong wrote: . . . Run the example program, for example: java SpriteTest2

Don't you need to quote the fully‑qualified name of the class?


That is the fully qualified name of the class. It's not in a package.
 
Campbell Ritchie
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's all right, then.
If it isn't in a package, why do we need ANT to compile it in the first place?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That's all right, then.
If it isn't in a package, why do we need ANT to compile it in the first place?


You don't, but maybe chapter 2 of the book talks about ant. There's no build.xml in chapter 1.
 
Frederick Pina
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. As part of my process to compile my Java Open-Source Codes, I've decided to update my OS X Mountain Lion's Ant. I followed these found online instructions.

http://wiki.eclipse.org/Ant/User_Guide#Updating_Ant_on_Mac_OSX

I downloaded the latest Ant 1.9.2 to my Downloads folders and renamed it Ant-1.9.2

Please tell me what's my error ?


Last login: Thu Sep 26 10:51:12 on ttys000
Fredericks-MacBook-Pro:~ frederickpina$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
Fredericks-MacBook-Pro:~ frederickpina$ /usr/share/java
-bash: /usr/share/java: is a directory
Fredericks-MacBook-Pro:~ frederickpina$ sudo mv ant-1.9.2 /usr/share/java/ant-1.9.2
Password:
mv: rename ant-1.9.2 to /usr/share/java/ant-1.9.2: No such file or directory
Fredericks-MacBook-Pro:~ frederickpina$ sudo ln -s -n -f /usr/share/java/ant-1.9.2/ /usr/share/ant
Fredericks-MacBook-Pro:~ frederickpina$ ant -version
-bash: /usr/bin/ant: No such file or directory
Fredericks-MacBook-Pro:~ frederickpina$

I'm under the impression that I didn't properly move my new Ant-1.9.2 folder to its proper location. Am I right ? And if so, how do I correct ?

Thanks





 
Campbell Ritchie
Marshal
Posts: 79961
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Us beginners don't know enough about ANT, so I shall move this discussion.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic