If Jesus knew Judas would betray him, why did Jesus entertain Judas until that time?
Paul addressed a similar question, though he looked back at a far older event. When it was time for the Israelites to be freed from bondage, people wondered why did God put such an evil man in as Pharaoh? After all, God is in control of the nations. "In order that the living may know that the Most High rules in the kingdom of men, Gives it to whomever He will, And sets over it the lowest of men" (Daniel 4:17). In this case, that was just the reason why God raised up a stubborn and evil man. "For the Scripture says to Pharaoh, "For this very purpose I have raised you up, that I may show My power in you, and that My name may be declared in all the earth."" (Romans 9:17). God wanted a stubborn and evil king in power so that He had an opportunity to display what true power was over the mightiest nation on earth at that time.
God made numerous prophecies about Jesus' life and death. He made such detailed statements so that we might realize that God's will is always done. "Remember the former things of old, for I am God, and there is no other; I am God, and there is none like Me, declaring the end from the beginning, and from ancient times things that are not yet done, saying, 'My counsel shall stand, and I will do all My pleasure,' calling a bird of prey from the east, the man who executes My counsel, from a far country. Indeed I have spoken it; I will also bring it to pass. I have purposed it; I will also do it" (Isaiah 46:9-11). The reason so much rich details were told in advance is to give us confidence in the power of God. "Thus God, determining to show more abundantly to the heirs of promise the immutability of His counsel, confirmed it by an oath, that by two immutable things, in which it is impossible for God to lie, we might have strong consolation, who have fled for refuge to lay hold of the hope set before us. This hope we have as an anchor of the soul, both sure and steadfast" (Hebrews 6:17-19).
In carrying out the details of God's prophecies, we find that sometimes a good person had to have done the right thing, such as when Joseph of Arimethea offered to bury Jesus in his family's tomb, it fulfilled a prophecy recorded by Isaiah 700 years before: "And they made His grave with the wicked - but with the rich at His death" (Isaiah 53:9). But other prophecies required the wicked to fulfill, such as Herod's slaughter of children trying to kill the infant Jesus, "Then was fulfilled what was spoken by Jeremiah the prophet, saying: "A voice was heard in Ramah, lamentation, weeping, and great mourning, Rachel weeping for her children, refusing to be comforted, because they are no more."" (Matthew 2:17-18; fulfilling Jeremiah 31:15). The point is that God controls what will happen, whether it involved the righteous or the wicked, whether it is a great thing or a small detail.
We sometimes forget that the Son of God knew why he came to earth. It was planned by God before the world was created. "Knowing that you were not redeemed with corruptible things, like silver or gold, from your aimless conduct received by tradition from your fathers, but with the precious blood of Christ, as of a lamb without blemish and without spot. He indeed was foreordained before the foundation of the world, but was manifest in these last times for you" (I Peter 1:18-20). Throughout the Gospels, Jesus told his disciples that it was necessary for him to die, but they could not believe it and would not listen to him. Shortly before it was time Jesus said, "Now My soul is troubled, and what shall I say? 'Father, save Me from this hour'? But for this purpose I came to this hour" (John 12:27).
I mentioned all of this to point out that Judas was selected because of his personality. Jesus knew he was a liar, a thief, and one willing to betray a friend if it would make him a quick buck. He needed such a man to accomplish what was planned. "Now His betrayer had given them a sign, saying, "Whomever I kiss, He is the One; seize Him." Immediately he went up to Jesus and said, "Greetings, Rabbi!" and kissed Him. But Jesus said to him, "Friend, why have you come?" Then they came and laid hands on Jesus and took Him" (Matthew 26:48-50). But a thousand years prior, David recorded for God, "Even my own familiar friend in whom I trusted, who ate my bread, has lifted up his heel against me" (Psalm 41:9).
Sometimes we have a hard time, like the disciples, comprehending that Jesus willingly gave his life for ours. He could have escaped, but he chose not to do so. He didn't have to associate with a man like Judas, but it was necessary for the ultimate sacrifice that he came to give. And that is the hard thing to comprehend. While Jesus was miserable about what he had to do, he did not shirk the duty laid upon him.