masijade 1,351 Industrious Poster Team Colleague Featured Poster

Your original approach, using Runtime should work just fine, and has got to be the easiest way to go. What exactly are you passing in as cmd, and what exactly is the error/exception?

Except, of course, that it varies from system to system and installation to installation and so, is only ever certain to work on that system on which it was developed, as long as the installation parameters don't change.

So, yes, should be easy to get working, but oooooo God is it dependable, huh?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Well, where the heck is kMeansPoint defined? Not in that code.

Also, if you are going to ask on help with an error, post the entire error and give at least some indication of where in the code that error takes place.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Well, it's not quite as easy as I make it sound, as you also have to read the classpath entries from the manifest file in order to start the URLClassLoader properly, but, if I get time in the next couple of days, I may, and I repeat may write one up for you (as a Generic class for launching other applications).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

The classloader is simply so that the executed jar (when you simply call main from the mainclass) has it's own classloader rather than having the classes that it loads, loaded into the rest of the application. Essentially, it is to give it it's own "sandbox", so that you can execute "internally", but still have it "separate" from the application that started it. You can also call main without creating a new classloader (as long as the jarfile is already on the classpath), but then any class loaded from the jar is "visible" to the entire application, which might not be want you want.

Ezzaral commented: Good info. +18
masijade 1,351 Industrious Poster Team Colleague Featured Poster

Well, if that's your real code, I can believe it's not "sending any errors", as you're ignoring them.

On the other hand, why don't you read the manifest file to get the Main class name, then start a thread, create a classloader, and call the main method from that main class using that classloader, rather than using runtime exec?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

The textfields themselves are probably null.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

AFAICT, the <sql:param> tag needs to be nested inside the <sql:query> tag.

Oops. ;-)

masijade 1,351 Industrious Poster Team Colleague Featured Poster
Where id LIKE ?
</sql:query>
<sql:param value="${param.id}"/>
masijade 1,351 Industrious Poster Team Colleague Featured Poster

1. Periodically. When, exactly, is irrelevant and varies from platform to platform. It is an implementation detail of the VM that the programmer doesn't really need to concern himself with. Also, System.gc(), and Runtime.getRuntime().gc() are only "suggestions" to the the VM that now would be a good time to do GC. The VM does not have to follow it though. It may do it then, it may do it after a delay, or it may ignore it altogether.

2. No. That's "humbug" to quote Ebeneezer Scrooge. There are some cases where it may be helpful, but they are real corner cases (i.e. very, very, very little instances) and, as long as the programmer is doing his job right, should never be a worry.

verruckt24 commented: thats a very good point that every one should be adviced on the use of System.gc() +2
masijade 1,351 Industrious Poster Team Colleague Featured Poster
masijade 1,351 Industrious Poster Team Colleague Featured Poster

As soon as an object is no longer referenced it will be garbage collected (at the next GC run). If the only reference to that list was in that method, then yes, that list will be eligable to be GCed as soon as the method returns. Not that that method will work in that manner, as you can only add objects, not primitives to a collection, and since you did not generitise that list with <Integer>, autoboxing will not happen (the VM does not autobox to Object, it autoboxes to relevant Number types).

masijade 1,351 Industrious Poster Team Colleague Featured Poster
masijade 1,351 Industrious Poster Team Colleague Featured Poster

well, first of all thanks for all the inputs. it is the code given by my professor and for us to modify and add things he wants us to add. so please do not assume too early people here are just lazy bums. i would say lazy student wouldn't even bother posting questions.

Well, I would say a non-lazy student would post the questions and his answers to them and ask for corrections and/or confirmation. Simply posting a homework assignment is lazy. It shows that the only effort you are willing to give to your assignment is to ask for somebody to do it for you, i.e. no real effort at all.

stephen84s commented: eggjactly +6
masijade 1,351 Industrious Poster Team Colleague Featured Poster

hi, i just have many questions regarding abstract class, please briefly explain if you may. thanks.

1. what is the difference between declaring public abstract class and abstract class ?

Well, what's the difference between declaring public class and class?

2. should abstract class have a constructor? i know by default it has one, but since abstract class doesn't have an object and its constructor cannot be instantiated, then why we need a constructor?

For subclass constructors to use, maybe, you think?

3. does abstract class normally have public methods?

An abstract class can and does have, just about, anything you want it to have.

4.does abstract class have concrete functions as well? and why would we have full function in an abstract class?

If most, if not all, subclasses would do the some of the same actions, then what is the point of each subclass having to define that action?


lastly, what is wrong with my Customer constructor() in the customer class?

abstract class CD 
	{
		double interest_rate;
		int months;
		
		public CD(double interest_rate, int months)
		{
			this.interest_rate= interest_rate;
			this.months= months;
		}
		
		abstract double calc_interest();// this method is not finished !!!
	
		public String toString()
		{
			return ("Interest Rate: "+ interest_rate + "\n" +
					"Months: "+ months +"\n");
		}
		
	}

	class Customer extends CD 
	{
		String name;
		String address;
		double deposit;
		
		// default constructor 
		public Customer()
		{
			name = "no name";
			address = "no address";
			deposit = 0;
		}
		
		// constructor 
		public Customer(String name, String address, double deposit)
		{ …
masijade 1,351 Industrious Poster Team Colleague Featured Poster

Ach, I meant to change that. It should, obviously, be

cir.computeArea();

not

child.computeArea();

but it doesn't need to casted. ;-)

masijade 1,351 Industrious Poster Team Colleague Featured Poster

But for that too computeArea() should be static.
OR
It should be called with the object name (cir) casted to child.

No, computeArea is declared in abstract1, so it perfectly legal to be called on an instance referenced as an abstract1 instance. And if the actual instance has overriden that method, then the overridden version will be used. This is how it is suppossed to work.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

I have the feeling what you actually meant to do, was this:

public class One {
    public static void main (String[] args) {
        int side=5;
        abstract1 cir = new child(side);
        child.computeArea();
    }
}

However, since computeArea is void, and you do not "print" anything from that method, you will get no visible clue (except that it produced no error) that it successfully ran.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Because "abstract1" does not declare that method, it cannot be called on an instance that is referenced as an abstract1 instance. Either you have to cast or declare it as "child" or you have to declare an abstract "child(side)" method in abstract1.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Set the Content-type and content-disposition headers and send the bytes through exactly as you read them.

Edit: nevermind that. Set your file/program associations in FireFox.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Look at the DeskTop class.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

No one told you to override paintComponent.

Load the image into a BufferedImage, get the Graphics2D, modify it, then create a ImageIcon from that.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Well, that's what you're telling to do (stay the original size, that is).

Try looking at the API docs for BufferedImage (and Graphics2D) and see if there is something there that can help you.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

This part of the code

class calculateButtonHandler implements ActionListener
	{
		public static calculateButtonHandler chandler;

is declaring an inner class, which cannot have static types.

Declare this class outside of the class definition for OfficeAreaCalculator.

P.S. Not that I think that that is the only problem with your code, but then again, I didn't really even look at it.

masijade 1,351 Industrious Poster Team Colleague Featured Poster
...
    private TextField x;
    ...
    public MyGUI() 
    {
        ....
        JTextField x = new JTextField(5);

The "textfield" you declare as an instance variable is not the same "textfield" you define in your constructor. And it is the one you declare that gets used in the actionPerformed method (which has never been defined, and so is null, per default).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

who knows?

<raking metal cup across iron jail bars/>
Nobody (at least the trouble I'm in)!

It might help to know a few more details.

What is it doing, that it should not be?
What is it not doing, that it should be?
What compiler messages are coming (post them in their entirety)?
What exceptions are coming (post them in their entirety)?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

I haven't gone through the entire program of yours, no need to even, since there is much of repeatition. But what I have gathered from a quick look is that you need to avoid making all those classes. You are doing the same thing in all of them. Why not then make 25 instances (threads) of the same class. You can pass a parameter to the thread specifying the IP range it needs to scan.

Um, all those classes have been replaced with a single "parametrisised" class, now. ;-)

I still don't agree with the approach though.

Personally, I would use 20 threads and the "isReachable()" method.

Edit: That's if I were forced to do it. Personally I wouldn't do it at all, but hey, he's probably got his reasons, and they may even be good ones.

Edit Again: @OP I have not looked at the "video", and probably never will. Don't mean to be mean, but it is, at the moment, too much involvment for me at the moment to go through all that. I can only assume that it has, at least partially, to do with the fact that you are doing a good amount of work on the event thread, and I still don't see where you are starting threads for the purpose of reading the output and error stream of the started processes, so it is still very much possible that a buffer is filling up.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Okay, so what do you have, so far?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

If you are on any Microsoft OS chances are that no such server exists on your system. You can also test the presence of an echo server by typing "telnet localhost 7" on your command line and see if it connects. If it displays "Connection Refused", then thats a confirmation that there is no echo server on your system.

And, if you're on a system owned by a company (rather than your own personal) then that server has probably been disabled, regardless of the OS. ;-)

(Most Security guidelines require the disabling of that service.)

masijade 1,351 Industrious Poster Team Colleague Featured Poster

And I told you that sort of thing would be a lot more work. Good luck with that, we are not going to write it for you.

For arrays:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

For Collections (which would be better):
http://java.sun.com/docs/books/tutorial/collections/index.html

caps_lock commented: thanks for the links +1
masijade 1,351 Industrious Poster Team Colleague Featured Poster

I'm fairly sure you meant "files.length" and not "f.length()".

Edit: Not that that matters though. Once the loop is done, the method will return anyway.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

The NPE (as the OP already knows from his post on www.java-forums.com (or is it org?)) is because listFiles will return null on an empty directory so he needs to add an if statement to check for that.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

post above: thanks for that, but I dont want to list the roots I want to list the files and files in directories that are in all directories on a computer.

Then what was this?

I want it to be able to go through all roots (drives)...

With no user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class.

As that is the part I was responding to. You will need to use that.

I think Ive got some stable code now. But I still want to know how to recursive scan the whole computer and not just a drive. At the moment I'm able to scan "C:\\" or "D:\\" and not both, does any one know how I can make that work?

See above. You can only "recursively" scan each drive/root one at a time. But you can loop over what the above mentioned method returns.

Why does no one ever listen to what I say?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

File has a method that returns all the system "roots". See the API docs for File.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

First of all, why 25 different ping classes? Simply create one with a constructor that takes parameters.

And then you start 25 threads that each start an additional 10 processes and you're wondering why it might take a bit of time?

And, you're performing all of these actions in the event thread (which should be worried about nothing other than updating the GUI).

It would probably be helpful to know exactly what "some modifications" means.

Also, the stdout and stderr have only so much buffer size, and if you fill those buffers then the app will hang until you read them. Read http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html completely and thoroughly.

Also, you're setting "scan_fin" true as soon as ping 25 is done (which doesn't necessarily mean they are all done, since 25 could run faster than some of the others and they are all running at the same time). And you should be synchronising access to that variable also.

And, currently, pushing scan does nothing as in this code you've commented out the only method call that would have done something.

You need to do some more general studying about the GUI event thread, threads in general, and Runtime Exec.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Use the

read(byte[], offset, length)

That method returns how many bytes were successfully read, or -1 at EOF.

Use that return value to modify the offset and the length and call read again.

Doing this in a while loop (i.e. != -1) will ensure your success (in the absence of an IO error, of course).

Try it and if you have a problem post that code.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Vernon, no disrespect intended there. I didn't pay any attention to who it was that was posting. Just feeling a little pissy, I guess. It happens. Sorry.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

> public static Color CopyAttributes (someclass bclass) C# bites? ;-)

Whoops, forgot to include the Color argument. Oh well, he should have been able to figure that one out himself (hopefully).

In any case, I agree with you. This duplication of data is stupid, and probably only done to avoid "having" to do getRed and the like, all the time. But, sometimes, I just don't feel like taking the effort to try talking them out of it. It is usually like "talking to a brick wall" and only half as effective.

;-)

masijade 1,351 Industrious Poster Team Colleague Featured Poster

I assume that these classes are all of the same type (or at least all extend the same type)? In that case simply pass the object itself along with the method call;

Instead of

public class aclass
{
    public static Color CopyAttributes (Color newcolor, int red, int green, int blue, int alpha)
    {
          red = newcolor.getRed ();
          green = newcolor.getGreen ();
          blue = newcolor.getBlue ();
          alpha = newcolor.getAlpha ();
          return new Color (red, green, blue, alpha);     
   }
}

do

public class aclass
{
    public static Color CopyAttributes (someclass bclass)
    {
          bclass.setRed(newcolor.getRed ());
          bclass.setGreen(newcolor.getGreen ());
          bclass.setBlue(newcolor.getBlue ());
          bclass.setAlpha(newcolor.getAlpha ());
          return new Color (red, green, blue, alpha);     
   }
}

Or define an interface with those setmethods and have the classes implement that interface, and then make the interface type the argument.

VernonDozier commented: Helpful approach. I hadn't though of passing a class or interface. +10
masijade 1,351 Industrious Poster Team Colleague Featured Poster

Well, if you're simply going to be reading the entire file into a single String without any kind of work on the lines read until after the entire file is read, I wouldn't use either of those, and wouldn't use a Reader at all. I would do it like this

File f = new File("filename");
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[f.length];
int read = 0;
while (read < b.length) {
  read += fis.read(b, read, b.length - read);
}
String text = new String(b);

This is, of course, missing all error handling. That's for you. This also assumes that encoding won't be problem.

You are not forced to use a Reader simply because what you're reading is a text file.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Assign that class instance to an instance variable, rather than a local variable, if you want to use that same instance throughout the class.

I.E.

public class A {
    private B bObject;

    A() {
      bObject = new B();
    }

    void whateverOne() {
        bObject.whatever();
    }

    void whateverTwo() {
        bObject.whatever();
    }
}

rather than

public class A {
    A() {
    }

    void whateverOne() {
        B bObject = new B();
        bObject.whatever();
    }

    void whateverTwo() {
        B bObject = new B();
        bObject.whatever();
    }
}
jbennet commented: fast reply +33
masijade 1,351 Industrious Poster Team Colleague Featured Poster

Like I said, try JSch.

masijade 1,351 Industrious Poster Team Colleague Featured Poster
Scanner myInput = new Scanner(System.in);
String input1 = "";
...
do {
  input1 = myInput.next();
  ...
} while (....

Split the declaration from the definition and put the definition as the first line of the loop.

Laidler commented: this post helped my fix my problem and was very helpful +1
masijade 1,351 Industrious Poster Team Colleague Featured Poster

now the

}while(!(my Input.equals(s4)));

does not work because the myInput variable is not stated outside of the loop

You're right my Input is not, but myInput is.

Unfortunatly "myInput" is a scanner, not a String, and you want to compare Strings, so aside from moving the "read" line into the while loop (but leaving the declaration of Input1 outside of the while loop), don't you think you should be using Input1 in that conditional expression?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

True, do not do this from a JSP.

If you feel you absolutely must do this (regardless of whether you do it from a JSP or not, and, again, you shouldn't do this from a JSP), check out JSch from JCraft (www.jcraft.com). It is a (nearly) complete implementation of SSH2 protocol as a Java API.

Also, why are you attempting to copy JSP's around? This seems to me, to be a hacked way of attempting to implement a distributed/clustered/HA application. If so, look into admin tools that are designed to do this (and they won't be executed from the application itself).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

what I meant by add line is 'append'. sorry for the misunderstanding, though. I thought that it is the common 'language'.

The thing is, there is no reason to read the file. You can create a FileWriter in append mode, so that anything written using it will be written to the end of the file, rather than overwriting the file (and if the file did not exist in the first place, it will still be created).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

so u know how to check whether or not the file is empty. Read the file then write it to your order.txt along with the new order.

that is the simplest way.

Uuuhhmm, what about "append"?

masijade 1,351 Industrious Poster Team Colleague Featured Poster

So take a look at liang's answer again. It has nothing to do with the if statement, it has to do with the fact that you are creating a new FileWriter with every "button-click", which will overwrite everything that's been written before it.

If you want to recreate the FileWriter every time then do it in append mode

new FileWriter(file, true);

And, you should be closing the writer in the finally block, and, as your code is now, even if there is an exception, the "success" message will still be shown (that should be in the try block, and the close in the finally block).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

In what way?

You do know, it is always helpful if you post exactly what it is that is not happening as expected (along with all compiler/error messages).

masijade 1,351 Industrious Poster Team Colleague Featured Poster

just change this

latestLine = read.readLine();
              System.out.println(latestLine);
            while(latestLine != null)
            {
                latestLine = read.readLine();

to

read = new BufferedReader(new FileReader(file));
            
            latestLine = null;
              System.out.println(latestLine);
            while((latestLine = read.readLine()) != null)
            {

It's because you are using the results of readline without first checking what it returned, and readLine returns null at EOF.

masijade 1,351 Industrious Poster Team Colleague Featured Poster

Maybe ask at a NetBeans Forum (www.netbeans.org maybe?)