Click here to Skip to main content
       

Java

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
GeneralRe: Best books/Tutorials to learn Java.?membermbatra3115-Nov-12 18:54 
Hi,
 
Thanx all for the info.
I want to know which IDE should I use for Java development. I have seen Eclipse Indigo, Galileo etc..I have used Eclipse Indigo.
 
Whats the difference between these IDEs and which should I use to start.
 

 
Thanx & Regards,
Mbatra
GeneralRe: Best books/Tutorials to learn Java.?memberGowtham Gutha16-Nov-12 7:13 
All that depends upon your needs. Different features differentiate different IDEs. What i could suggest is that go with what you feel the best
 
In my experience, personally i use NetBeans for developing Desktop applications as i feel it comfortable and also clean. It is simple to learn too.
 
And for web development, i prefer Eclipse Indigo, because i have personally used it for developing my iGo4iT Search because it has provided me a plugin to deploy apps to Google AppEngine. Eclipse really do have a wide range of plugins that are useful for the developers, especially for the web application developers. You can also take Amazon Beanstalk plugin for Eclipse for example. It is helpful when you want to deploy your applications to the Amazon servers.

GeneralRe: Best books/Tutorials to learn Java.?memberGowtham Gutha16-Nov-12 7:14 
All that depends upon your needs. Different features differentiate different IDEs. What i could suggest is that go with what you feel the best
 
In my experience, personally i use NetBeans for developing Desktop applications as i feel it comfortable and also clean. It is simple to learn too.
 
And for web development, i prefer Eclipse Indigo, because i have personally used it for developing my iGo4iT Search because it has provided me a plugin to deploy apps to Google AppEngine. Eclipse really do have a wide range of plugins that are useful for the developers, especially for the web application developers. You can also take Amazon Beanstalk plugin for Eclipse for example. It is helpful when you want to deploy your applications to the Amazon servers.
Gowtham Gutha (Java-Demos.Blogspot.Com)

QuestionFIX Day Changed ProblemmembertechGaurav8-Oct-12 21:42 
in FIX i face the problem on Day changed my Fix message like this
 
client logon message
server logon response
Resend Request with Beg and End Seq numb value =0
Sequence Reset Response from Server
heart beat from client side
Test request from client side
heart beat from server side
then unexpected Dicconnect EVENT fire
AnswerRe: FIX Day Changed ProblemmemberTorstenH.8-Oct-12 22:00 
are you talking about the http://www.fixprotocol.org/[^]?
regards Torsten
When I'm not working

AnswerRe: FIX Day Changed ProblemmemberNagy Vilmos11-Oct-12 20:12 
This has nothing to do with Java and to be honest it may not be anything to do with day change.
 
This depends on the server implementation. I would see what happens if you delay the test request until after the receipt of the heat beat from the server. If that works, I would then look at checking the server side.
 
Failing that, reply here with the message stack and I may be able to point you in the right direction.


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

QuestionThreads in detailsmemberCsTreval6-Oct-12 0:16 
See the following code:
Unsafe Counter
public class Counter implements Counting
{
    private int value = 0;
 
    public int incrementAndGet()
    {
        return this.value++;
    }
 
    public int decrementAndGet()
    {
        return this.value--;
    }
}
CounterClient
public class CounterClient implements Runnable
{
    //shared data    
    private Counting counter;
 
    public CounterClient(Counting counter)
    {
        this.counter = counter;
    }
 
    public void run()
    {
        while(true)
        {
            System.out.println("Incrementing from " + Thread.currentThread().getName() );                
            System.out.println("Counter value in " + Thread.currentThread().getName() + " = " + counter.incrementAndGet());                
            
            try
            {
                Thread.sleep(2000L);
            } catch (InterruptedException ex)
            {
                Logger.getLogger(CounterClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}
TestCounter
public class TestCounter
{
    public static void main(String args[])
    {
        Counter counter = new Counter();
        SafeCounter safeCounter = new SafeCounter();
        
        Executor exec = Executors.newCachedThreadPool();
        
        exec.execute(new CounterClient(counter));
        exec.execute(new CounterClient(counter));        
        /*
        exec.execute(new CounterClient(safeCounter));
        exec.execute(new CounterClient(safeCounter));
         */
         
    }
    
}
 
I did a test run for this code and one of the outputs was this:
Incrementing from pool-1-thread-1
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-1 = 0
Counter value in pool-1-thread-2 = 1
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-2 = 2
Counter value in pool-1-thread-1 = 3
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 4
Counter value in pool-1-thread-2 = 5
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 6
Counter value in pool-1-thread-2 = 7
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 8
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 9
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 10
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 11
Incrementing from pool-1-thread-1
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 13
Counter value in pool-1-thread-1 = 12
 
I am trying to understand what exactly is going on in the thread activity here that is not thread safe. As you can see, some values are out of order: e.g. 13,12
 
This is what I can deduct from the output:
Thread-1 increments (value++)
Thread-2 increments (value++)
 
Thread-1's value should be 1 now, but I see this:
Counter value in pool-1-thread-1 = 0
Is this because thread-1 is still reading and didn't write the result of the register back to memory?
 
Why is it 0? Didn't it just increment?
Thread-2's value is 1. That's logical.
 
This is all a bit confusing to me and I would like to know exactly what's going on in that output.
 
Thanks
AnswerRe: Threads in detailsmemberNagy Vilmos8-Oct-12 0:54 
The code you have will return the value then increment, or decrement. To perform the change before hand try:
return ++this.value;
 
It is not really thread safe, but it is such a trivial example that you won't see a problem.


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

Questionjava applet security issuesmemberPonnampi5-Oct-12 18:01 
Ok I'm having this issue with my applet here. I compile it and run it and it always throws an exception. 'java.security.AccessControlException: access denied (java.net.SocketPermission asylym-mud.org connect, resolve)'
 
I've looked up many different possible solutions and the bulk of them state that I need to genereate a certificate for it and assign it via the 'grant' code to the 'jre/lib/security/java.policy' file. I've done all this and it still gives me the same exception. Any ideas anyone?
 
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.TextField.*;
import java.lang.*;
import java.util.*;
 
public class AMClient extends JApplet implements ActionListener{
	static public Socket localSocket;
	static final long serialVersionUID = 42L;
    static public PrintWriter out;
    static public InputStream in;
    static public JFrame fAMClient;
	static public JPanel nPanel,cPanel,sPanel;
    static public JButton btnConnect;
    static public JTextField tfInput;
	static public JTextArea taMud;
 
	public void init()
    {
		        try
		        {
					System.out.println("1");
					System.getSecurityManager().checkPermission(new SocketPermission("asylum-mud.org", "connect"));
					localSocket = new Socket("asylum-mud.org", 6715);
					System.out.println("2");
					in = new BufferedInputStream(localSocket.getInputStream());
					System.out.println("3");
					out= new PrintWriter(localSocket.getOutputStream(),true);
				}
				catch(Exception e)
				{
					System.out.println(e);
		}
		tfInput= new JTextField(30);
		btnConnect = new JButton("Connect");
		taMud = new JTextArea(30,50);
		add(btnConnect);
		add(taMud);
		add(tfInput);
		//display d = new display();
        //getContentPane().add(d);
        btnConnect.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
		//makes sure source is the connect button
		//& disable the button so it won't call
		//upon itself while active.
        if(e.getSource() == btnConnect)
        {
			btnConnect.enable(false);
			//btnConnect.setVisible(false);
		}
        //Create the socket to asylum-mud.org 6715
        try
        {
			//While we have a connection
            // get user input value and store it as a string
			taMud.append("6");
			String stdInn;
			taMud.append("7");
			stdInn = slurp(in,4122 );
			taMud.append("8");
			taMud.append(stdInn);
			taMud.append("9");
			out.flush();
        }
        catch(Exception ioe)
        {
            System.out.println(ioe.getMessage());
        }
    }
 
    public static void main( String[] args )
    {
        try
        {
			System.out.println("1");
			localSocket = new Socket("asylum-mud.org", 6715);
			System.out.println("2");
			in = new BufferedInputStream(localSocket.getInputStream());
			System.out.println("3");
			out= new PrintWriter(localSocket.getOutputStream(),true);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		fAMClient = new JFrame();
		System.out.println("init 1");
		JApplet aAMClient = new JApplet();
		aAMClient.init();
 
		fAMClient.getContentPane().add(aAMClient);
		fAMClient.setSize(1200, 1200);
        fAMClient.setVisible(true);
        fAMClient.setTitle("AbsentMirage Client");
        aAMClient.start();
    }
 
    public static String slurp(final InputStream is, final int bufferSize)
	{
		final char[] buffer = new char[bufferSize];
	    final StringBuilder slurpOut = new StringBuilder();
	    taMud.append("slurp1");
	    try
	    {
			taMud.append("slurp2");
		    final Reader slurpIn = new InputStreamReader(is, "UTF-8");
	        taMud.append("slurp3");
	        try
	        {
	             final int rsz = slurpIn.read(buffer, 0, buffer.length);
	             slurpOut.append(buffer, 0, rsz);
	        }
	        catch (IOException ex)
	        {
				System.out.println(ex);
	    	}
	    finally
	    {
			try
			{
				in.close();
			}
	        catch (IOException ex)
	        {
				System.out.println(ex);
			}
	    }
	  }
	  catch (UnsupportedEncodingException ex)
	  {
		  System.out.println(ex);
	  }
	  return out.toString();
	}
 
}

AnswerRe: java applet security issuesmemberPonnampi5-Oct-12 18:12 
ok I think I've figured out what it is... I haven't uploaded my applet to asylum-mud.org yet and have been trying to test it locally on my machine.
QuestionData Structures - ArrayDeque <T> extends AbstractList <T>memberMember 94884535-Oct-12 16:29 
I'm trying to implement a Data structure in Java that is an ArrayDeque without using modular arithmetic. When testing my code I get an error:
 
java.lang.ArrayIndexOutOfBoundsException: 16
at a2checker.SimpleTest.test(SimpleTest.java:70)
at checker.AtomicTest$RunnableTest.run(AtomicTest.java:70)
at java.lang.Thread.run(Thread.java:636)
 
So it would seem that part of my code manipulating arrays a[X] is trying to do something with an index 'X' that is out of bounds. I have been staring at my code and cant see what is wrong with it. Any input would be greatly appreciated.
 
Here is my code.
---------------------------------
 
 
import java.util.AbstractList;
 
/**
* An implementation of the List interface that allows for fast modifications
* at both the head and tail.
*
* @param <T> the type of objects stored in this list
*/
public class ArrayDeque2<T> extends AbstractList<T> {
    /**
     * The class of elements stored in this queue
     */
    protected Factory<T> f;
 
    /**
     * Array used to store elements
     */
    public T[] a;
 
    /**
     * Index of next element to de-queue
     */
    public int j;
 
    /**
     * Number of elements in the queue
     */
    public int n;
 
    /**
     * Grow the internal array
     */
    protected void resize() {
        T[] b = f.newArray(Utils.max(2*n,1));
        for (int k = 0; k < n; k++)
            b[k] = a[(j+k >= a.length) ? j+k-a.length : j+k];
        a = b;
        j = 0;
    }
 
    /**
     * Constructor
     */
    public ArrayDeque2(Class<T> t) {
        f = new Factory<T>(t);
        a = f.newArray(1);
        j = 0;
        n = 0;
    }
 
    public int size() {
        return n;
    }
 
    public T get(int i) {
        if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
        return a[(j+i >= a.length) ? j+i-a.length : j+i];
    }
 
    public T set(int i, T x) {
        if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
        T y = a[(j+i >= a.length) ? j+i-a.length : j+i];
        a[(j+i >= a.length) ? j+i-a.length : j+i] = x;
        return y;
    }
 
    public void add(int i, T x) {
        if (i < 0 || i > n) throw new IndexOutOfBoundsException();
        if (n+1 > a.length) resize();
        if (i < n/2) {
            // shift elements 0,...,i-1 left in a
            j = (j == 0) ? a.length - 1 : j - 1;
            for (int k = 0; k < i-1; k++)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k+1 >= a.length) ? j+k+1-a.length : j+k+1];
        } else {
            // shift elements i,...,n-1 right in a
            for (int k = n; k > i; k--)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k-1 >= a.length) ? j+k-1-a.length : j+k-1];
        }
        a[(j+i >= a.length) ? j+i-a.length : j+i] = x;
        n++;
    }
 
    public T remove(int i) {
        if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
 
        T x = a[(j+i >= a.length) ? j+i-a.length : j+i];
        if (i < n/2) {
            for (int k = i; k > 0; k--)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k-1 >= a.length) ? j+k-1-a.length : j+k-1];
            j = (j+1 >= a.length) ? j+1-a.length : j+1;
        } else {
            for (int k = i; k < n-1; k++)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k+1 >= a.length) ? j+k+1-a.length : j+k+1];
        }
        n--;
        return x;
    }
 
    public void clear() {
        a = f.newArray(1);
        n = 0;
        resize();
    }
}

AnswerRe: Data Structures - ArrayDeque extends AbstractListmemberTorstenH.5-Oct-12 21:36 
Do you use a decent IDE like Eclipse or Netbeans?
 
- switch line numbers on (preferences of eclipse/netbeans)
Because the exception is clearly happening in line 70 of your class SimpleTest - which I assume is a Unit Test.
 
- debug with a break point on ArrayIndexOutOfBoundsException.
the debugger will stop at that point automatically.
regards Torsten
When I'm not working

QuestionCreating a class that extends more than one class.memberBill.Moo4-Oct-12 2:43 
Hi, I'm from a C++ background and I need to create a class that extends both AbstractAction and Observable, but this of course can't be done 'out of the box' using Java.
 
So I would appreciate from those more experienced than me as to how I can achieve this, assuming it even can be.
--
Bill

AnswerRe: Creating a class that extends more than one class.memberNagy Vilmos4-Oct-12 3:00 
You need to impliment interfaces rather then extend classes. A single class can only extend one super class, but you can implement as many interfaces as you like. The difficulty here is that you need to code every method:
 
class Top {
   void doSomething() {
     // something is done
   }
}
 
interface First {
   void goFirst();
}
 
interface Last{
   void goLast();
}
 
class Bottom
      extend Top
      implement First,
            Second {
 
   void doSomething() {
     this.goFirst();
     super.doSomething();
     this.goLast();
   }
 
   void goFirst() {
      // the first bit;
   }
   void goLast() {
      // the last bit;
   }
   
}
 
Simples!


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

GeneralRe: Creating a class that extends more than one class.memberBill.Moo4-Oct-12 3:48 
Thanks for that, I'll do some reading on the matter.
--
Bill

AnswerRe: Creating a class that extends more than one class.memberPeter_in_27804-Oct-12 3:03 
The "Java way" is to use interfaces to achieve (most of) multiple inheritance.
AFAIK, the "Observable/Observer" concept has been pretty much obsoleted by events and actions, so maybe you don't need to do what you can't.
 
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

GeneralRe: Creating a class that extends more than one class.memberBill.Moo4-Oct-12 3:59 
Where do you read this? I got the Observable/Observer from the "Java - The complete reference 8th Ed. Ch 18, p541" and there is no reference to it being obsolete or as a candidate for it either.
 
What I want to do is create a class that extends AbstractAction and implements runnable. The actionPerformed will display a file open dialog allowing the user to select N files, these files are then stored in a File[] and the thread started when the dialog is closed to process the files. But I wanted the observable so I could 'emit' messages to the Observer (my UI) so I could say "Processing filename.ext..." as the processing can take a bit of time, hence the use of the thread and 'status message'. But of course if there is a better way of doing this then I'm all for learning.
 
The Events / Actions sounds sensible, are you able to give me a
link to something?
--
Bill

GeneralRe: Creating a class that extends more than one class.memberPeter_in_27804-Oct-12 11:36 
Blush | :O That's what I get for trying to make sense at midnight. I use "Java in a Nutshell, 5th ed". Observable/Observer are since Java 1.0 and the comment is "The Observable class and Observer interface are not commonly used. Most applications prefer the event-based notification model defined by the JavaBeans componenent framework and by the EventObject class and EventListener interface of this package. [java.util]" So I misread preference as superceding... I do remember the Event model being introduced (in Java 1.1) in response to a perceived hole in functionality.
Looks like you want to make your UI implement EventListener and have the worker thread spit out (your subclass of) EventObjects as required.
I'm sure there are plenty of examples floating around the web. (Java's like that Smile | :) ) I'd start with Mr G and something like "Java EventObject".
 
Good hunting,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

AnswerRe: Creating a class that extends more than one class.memberpasztorpisti4-Oct-12 12:14 
Before starting any serious java tutorials its worth reading through the official beginner java tutorial: http://docs.oracle.com/javase/tutorial/index.html[^]
Read at least the "Trails Covering the Basics" paragraph and all of its subpages. Its a novice coder tutorial so you will progress with that quickly and you can gain a lot of important java-specific info from it that makes your work much easier. It worth at least a quicky reading even if you are lazy to try the examples!
QuestionRe: Creating a class that extends more than one class.memberMonster Maker21-Oct-12 21:08 
In java, multiple inheritance concept can only be accomplished through interfaces. I do it using the concept of adapter classes.
interface Dog
{
	public void bark();
}
 
interface Cat
{
	public void meow();
}
 
class DogCat implements Dog,Cat
{
	public void bark()
	{
		System.out.println("Barking");
	}
 
	public void meow()
	{
		System.out.println("Meowww");
	}
}
 
public class Hybrid extends DogCat
{
	public static void main(String[] args)
	{
		Hybrid r =new Hybrid();
		r.bark();
		r.meow();
	}
 
}

AnswerRe: Creating a class that extends more than one class.memberGowtham Gutha15-Nov-12 6:59 
Multiple inheritance for classes is however not supported by Java unfortunately. It is limited for interfaces only. Alternatively, you can use multi level inheritance that gets the same as the features of the multiple ones. Here is a simple trick that you can do to achieve this..
 
Let the super classes be S1,S2
 
class A extends S1
{
 
}
class B extends S2
{
}
class C
{
// write the code here by creating objects for A,B
// This could be good if S1,S2 are abstract and that
// you have implemented the abstract methods in the
// classes A,B. So you can create objects for A,B
// access methods in them (the implemented ones) and // also the normal ones.
}
Gowtham Gutha (http://java-demos.blogspot.com)

QuestionThread namesmemberCsTreval4-Oct-12 1:19 
App.java
Thread runnable1 = new Thread(new MyThreadRunnable(), "runnable1");
runnable1.setName(runnable1.getName());
runnable1.start();
MyThreadRunnable.java
public class MyThreadRunnable implements Runnable {
    private String name;
 
    public void setName(String name){
        this.name=name;
    }
 
    public void run() {
        System.out.println("Testing MyThreadRunnable"+" "+name);
    } 
}
 
Why does line 2 of App.java result in a 'null' for the name member of MyThreadRunnable? Is it not so that I just created a new Thread, passed a Runnable to it and gave it a name? Is it not supposed to be so that the name property should carry the value of Thread's getName()?
 
I don't understand.
AnswerRe: Thread namesmemberNagy Vilmos4-Oct-12 2:54 
Simply put you are addressing the Thread method not the MyThreadRunnable method. You could change your code thus and it would work as expected:
 
MyThreadRunnable runnable = new MyThreadRunnable();
Thread thread = new Thread(runnable, "runnable1");
runnable.setName(thread.getName());
thread.start();


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

AnswerRe: Thread namesmemberGowtham Gutha15-Nov-12 7:11 
You've already set the name to the thread in the constructor itself. Why are you again setting the same.
This works fine. It probably will not return null.
 
You'll need to call setName() on MyRunnable object and not on Thread object. By the way, the setName() method in MyRunnable object is not at all a name for the thread. It might be just for printing purposes. Do you want it like that only?
 
Remove runnable1.setName(thread.getName()), try calling the setName() method in the class that you've written (MyRunnable).
 
It should work! Test once.
Gowtham Gutha (http://java-demos.blogspot.com)

Questionget ethernet IP addressmemberhari3013-Oct-12 19:30 
I've tired to get exact ethernet ip address.
 
InetAddress ip=InetAddress.getLocalHost();
String iaddr = ip.getHostAddress();
 
It return localhost address only. I need ethernet address only..
 
pls help me..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 18 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid