Easy way to compile and execute java programs?

Hi,

I think the most frustrating thing about learning java is running programs.

  1. I am running my programs in a DOS window, and when I first started, it took me several days to straighten out the PATH to refer(?) to a directory where I could run programs. (See: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/ where there is a link on how to update the PATH variable)

  2. Then, to run a program, I had to open a DOS window and change the prompt to the proper directory:

cd C:\javaPrograms

After awhile, I figured out how to make C:\javaPrograms the default prompt, so that saved me from having to change the prompt everytime I opened up a DOS window(in a DOS window click on the Properties icon, and on the Programs tab next to “Working:”, list the default prompt you want)

  1. To run a program, I had to type:

javac DemoClassBook.java

and if that was successful, I had to type:

java DemoClassBook

If I made any mistakes, which I frequently did, I had to retype all that junk. I use a commercial text editor called EditPlus, and I finally figured out a way to to set up a couple of buttons on the editor to grab the filename and type those commands for me. So, things were starting to get tolerable.

  1. Now, I am learning about packages, and I am right back to where I started. As far as I can tell, for a simple program like this:
package Pack1;

class A
{
	protected int number;
	public A(int num)
	{
		number = num;
	}
}

class DemoA
{
	public static void main(String[] args)
	{
		A a = new A(100);
		System.out.println(a.number);
	}
}

I have to create a sub folder called Pack1 and put my file containing A and DemoA in there, then change the DOS prompt to the Pack1 directory:

cd C:\javaPrograms\Pack1

then once gain hand type(instead of being able to use my buttons in EditPlus):

javac DemoA.java

and if that works, then I have to switch back to:

cd C:\javaPrograms

and finally my program runs when I type:

java Pack1.DemoA

What the heck!!!?? All that typing to run a program? There has to be a better way!! Doesn’t there?

Did you ever try pressing the up arrow in your dos window (or F3)? You may find it useful.

otherwise, if you really want to get into Java you should try an IDE, I use Eclipse, I think it’s the best one out there. A lot of people seem to like Netbeans as well. I’ve tried it out, it has some similarities to Eclipse. They’re both good and they’re both free.

Eclipse: http://www.eclipse.org
Netbeans: Somewhere on http://java.sun.com (search for Netbeans)

Using any of these will make it a lot easier to manage Java projects, things like classpath and packages. I actually use EditPlus as well for quick edits but once you start writing Java in a real IDE you won’t go back to the simple text editor (although I really like EditPlus as a text editor).

Uh, no. If you want to get into an IDE, get an IDE, if you want to get into Java, stick with what you have and learn to use it.

I don’t use EditPlus, so I don’t know what it has available for certain, but I suspect you can configure it to be a bit more flexible than your current settings.

Understanding what information java and javac need to run properly will help you in that regard. (as will reading the EditPlus documentation)

javac:

  • if you’re using a package other than a java package, you’ll need a classpath.
    if you’re compiling outside out the directory that has the source file, you’ll also need the path to the directory that has the top of the package structure.

  • ie: the classpath for some jar, just to show the classpath:
    -classpath “c:\path\ o\a\jar\some.jar”

  • note: jars will have to be included individually, you can’t just specify the directory

  • the the path to, and the java filename: “C:\java\pack\MyJava.java”

  • the quotes are only needed when there are spaces within the path or filename, good practice to always use them (for any path)

java:

  • classpath to the top of the package structure containing your class, we can shorten it to cp for java, and the jar containing the imported classes (you’ll want to know about including jars eventually):

    -cp “c:\java”;“c:\path\ o\a\jar\some.jar”

  • the fully qualified name of the class, this means: including the package structure, but without the .class extention: “pack.MyJava”

Next: The BAT file is your friend.

A bat file can be nothing than other than a dos command you would type, but handily store in a file so you don’t have to type it over and over and over

If you can’t get EditPlus to cooperate, use bat files. My favorite scheme is:

c.bat - the javac statement
c:\path\ o\java\bin\javac.exe -classpath “C:\java”;“c:\path\ o\a\jar\some.jar” “C:\java\pack\MyJava.java”

r.bat - the java statement
c:\path\ o\java\bin\java.exe -cp “c:\java”;“c:\path\ o\a\jar\some.jar” “pack.MyJava”

go.bat - invokes both c.bat and r.bat
c.bat
r.bat

so, edit .java, save, go to command promt, type c, then r, or just go

Hey 7stud … nice to see you over in the Java section!

I use edit plus for ALL my Java … and compiling in it is as simple as cntrl 1 then to run cntrl 2… works great and is a REAL time saver.

You should really try it … it only $29 (last time I checked) and give you syntax colors etc… and it has almost NO learning curve, which a LOT of the bigger IDEs have. (lets face it you want to learn Java right now NOT some overblown IDE) Yeah Eclipse is great so dont beat me up but for someone learning the nuts and bolts of java I dont recommend it.

To set it up to compile all you have to do is go into tools > preferences, user tools (under the Tools subheading) > add tools and then find javac.exe as the command, argument (filename), initial directory as File Directory…

Same thing with running them except the exe is java and the argument is filename without extension…

So literally but then hitting cntrl 1 / cntrl 2 you have compiled and the app runs in a dos window IT opens! No more typing it long drawn out paths.

I have to agree tying all that crap into a DOS window REALLY bites!

Hi,

Thanks for the responses.

7stud:
I use a commercial text editor called EditPlus, and I finally figured out a way to to set up a couple of buttons on the editor to grab the filename and type those commands for me. So, things were starting to get tolerable.

dc dalton:
I use edit plus for ALL my Java … and compiling in it is as simple as cntrl 1 then to run cntrl 2… works great and is a REAL time saver.

You should really try it … it only $29 (last time I checked) and give you syntax colors etc… and it has almost NO learning curve, which a LOT of the bigger IDEs have. (lets face it you want to learn Java right now NOT some overblown IDE)

I have EditPlus. As I tried to explain, I had some user tools set up that worked. I think I have the user tools configured like you described:

javac

Command:C:\j2sdk1.4.2_06\bin\javac.exe
Argument:$(FileName)
Initial Directory:$(FileDir)

java

Command:C:\j2sdk1.4.2_06\bin\java.exe
Argument:$(FileNameNoExt)
Initial Directory:$(FileDir)

The problem is, now that I am using packages, they don’t work. And, I don’t see how they could. To run a package, supposedly, you have to use a command like this:

java Pack1.DemoA

for a file named “DemoA.java” with a package statement:

package Pack1;

at the beginning of the file, and the file “DemoA.java” has to be saved in a folder called Pack1.

How is $(FileNameNoExt) going to produce “Pack1.DemoA”? I think that will only produce: “DemoA”. Should I be naming files in packages with the Package name, e.g. “Pack1.DemoA.java” instead of “DemoA.java”?

well you got me there … I tried it and indeed the compile works but it wont run becuase it cant find the class file within the package.

I tried adding the path to the CLASSPATH variable - no luck

I tried adding c:\java_stuff\pack1\ , c:\java_stuff\pack1 , c:\java_stuff\ & c:\java_stuff to the initial directory for java and it didnt find it… I honestly dont know!

I dont do any regular java apps so this has never been a problem for me. I am always working in my local Resin directory in JSPs or Servlets … that container knows how to find packaged class files so again it never came up.

Should I be naming files in packages with the Package name, e.g. “Pack1.DemoA.java” instead of “DemoA.java”?

No thats not going to work - the compiler is going to raise hell about it!

Thats a heck of a question and Ill see if I can find something about it today but at this point Im at as much of a loss as you are!

The solution here is simple.

Have your editor launch Ant. Have Ant compile the program. Then run the program.

I seen your comment on the recent Ant article 7stud, so you should be able to figure out a good resource :wink:

ok, well I do agree that an IDE such as Eclipse is quite heavy if you’re just learning the basics. Thinking back, when I first started with Java it was with a text editor called TextPad, I then later moved to EditPlus and eventually started working with Eclipse.

I’m not sure how you can set up the packages to run with Editplus as I never tried that when I was using it for Java, but as Rushiku said, use batch files. They are good.

If your java file is in:

package pack1;

and the class is called

public class Class1

you need a file called Class1.java under the directory structure of

pack1\Class1.java

this can be run from anywhere (command prompt) like so:

<java_home>\bin\java.exe -cp <whatever cp is necessary> <root_of_your_code>\pack1.Class1

This is much easier accomplished by setting up your path with:

path=%path%;c:\j2sdk1.4.2_05\bin (or wherever your java home is)

and then running from the root directory of your project with.

java -cp <cp stuff> pack1.Class1

If your code is just basic Java you probably don’t need the -cp flag. You should only need that for external/third party Jars.

And doskey (now integrated into windows command) is your friend for avoiding retyping long commands, use the up arrow to scroll through and edit previous commands as necessary.

Good luck with Java, I hope you get it working. It is a great language to learn.

Thanks for the help.

I dont do any regular java apps so this has never been a problem for me. I am always working in my local Resin directory in JSPs or Servlets … that container knows how to find packaged class files so again it never came up.

…darn it.

I seen your comment on the recent Ant article 7stud, so you should be able to figure out a good resource
I couldn’t understand the first sentence of that article, but I’ll try reading the rest of it. :frowning:

Thinking back, when I first started with Java it was with a text editor called TextPad, I then later moved to EditPlus and eventually started working with Eclipse.
…yeah, somehow yesterday when I was trying to figure everything out, I ended up at the Textpad site–some link said it was a free editor that was great for Java, but it cost money just like Edit Plus.

…but as Rushiku said, use batch files.
Ok, I’ll try that when I have some time. Thanks. I’m sure I’ll have more questions later.

There’s a very simple IDE that you can get from Auburn University called JGrasp
www.jgrasp.org

There’s a very simple IDE that you can get from Auburn University called JGrasp
www.jgrasp.org

Pretty nice, this is also similar to JEdit, http://www.jedit.org

javac:
if you’re compiling outside out the directory that has the source file, you’ll also need the path to the directory that has the top of the package structure.
Ok, I tried compiling from a different directory than the one containing the source file, and it worked :agree:, but that is waaaayyy too much typing.
====

This is much easier accomplished by setting up your path with:

path=%path%;c:\j2sdk1.4.2_05\bin (or wherever your java home is)

and then running from the root directory of your project with.

java -cp <cp stuff> pack1.Class1

I have this in my autoexe.bat:

PATH C:\J2SDK1.4.2_06\BIN
c:\maestro.com

Is there supposed to be an equals sign? In any case, with this structure:

C:\javaPrograms\Pack1\DemoA.java

I can run(v. compile) the program like this:

C:\javaPrograms> java Pack1.DemoA

Did you ever try pressing the up arrow in your dos window (or F3)?
I don’t have any arrows in my DOS window, and the arrows on my keyboard don’t seem to do anything. Hitting F3 looks like it copies that last command. I’m not quite sure how that can be employed in a useful way: as far as I can tell, you can’t insert characters, so you have to backspace and erase part or all of the command.
======

Understanding what information java and javac need to run properly will help you in that regard. (as will reading the EditPlus documentation)

javac:

  • if you’re using a package other than a java package, you’ll need a classpath.

“…a package other than a java package”?? What does that mean?

go.bat - invokes both c.bat and r.bat

How do you make go.bat invoke c.bat and r.bat?

If you can’t get EditPlus to cooperate, use bat files.

So, essentially everytime you want to compile and run a program, you have to add a new classpath to the .bat file?

And doskey (now integrated into windows command) is your friend for avoiding retyping long commands, use the up arrow to scroll through and edit previous commands as necessary.

I’m using win98, and my arrows don’t seem to do anything.

Thanks for the links to those editors. I think I’ll have to give one of those a try…stupid Java. :confused: How did more than 6 people decide to use this language?

I downloaded jEdit, but I can’t figure out how to compile a program. I read the help, and it says jEdit is just a text editor unless you download additional plugins–of which there are many. Any ideas what plugin I need?

I didn’t realize you were using Windows 98, if you type in doskey in the command line it should say “Doskey installed”, then you can press the up arrow to scroll through previous command and you can press insert to modify previous commands.

I haven’t actually used Jedit in years, I just remembered seeing it and it was similar to the other one that bullet45 mentioned, so I can’t really help you there.

For your batch file go.bat, you want something like:

@echo off
call c.bat
call r.bat

How did more than 6 people decide to use this language?
Well, once you get past the little complicated issue it’s quite a powerful language.

I think I’ll have to give one of those a try…stupid Java. How did more than 6 people decide to use this language?

Now Now … patience there 7stud. Java is on of THE toughest languages to learn and master (if you ever master it that is) …just like anything else, time and patience!

BTW, I did want to ask you … what are you trying to build? Sounds like a GUI or something? To be very honest if you are just learning Id focus on the language and not frustrate yourself with the package stuff … that CAN come later and if you are working towards doing web apps you’ll NEVER have this problem anyways because your web app container handles it for you!

Keep up the faith … it gets better SO MUCH BETTER!

Heh ya, learning Swing without a good knowledge of Java already isn’t fun. :slight_smile:

Thats what we had to do in our first term. Needless to say my swing app was god-awful and slowed up a P4 2.0ghz because of its use of threads all trying to run at 100% :rolleyes:

OH Ive seen classes like that! OMG is THAT painful! That’s also why I NEVER recommend the Deitel & Deitel book for beginners! Man the concepts of java can be difficult enough without adding the pain and suffering of Swing … Ive done it but MAN I dont like it!

7stud: If you just want to learn the basics of Java I would HIGHLY recommend doing just that … skip the packages and the Swing until you have a firm grasp of the core language! There enough to learn without making yourself NUTZO with that!

To be very honest I dont even see that much Swing work around anymore (may not be looking hard enough I might add!) Most of what I see is web app stuff.

Ya, with all the java programming I’ve had to do, i’ve never had to do any Swing work, just some background/console apps and web app work (struts, JSTL etc).
The work I’ve done with Swing was by choice (I’m not as crazy as I sound, I swear! :rofl: )

I made a Tetris game in Swing, other than that I used it for a few school assignments. Currently I’m doing some work in SWT which I found even harder to learn than Swing (if you can believe that) but is actually quite nice to work in after understanding how it all works. Very nice results with SWT. But most of my Java work nowadays is J2EE. Java is definitely king of the web, I have little interest in going back to PHP or ASP.