0

First, sorry if question is trivial - I'm new to java world.

So, I've created new project. Then I downloaded websocket jar file here:

http://www.java2s.com/Code/Jar/j/Downloadjavawebsocket130jar.htm

After that I've created 'lib' dir, copyed jar file there, selected it, added to build path.

Basically, I was following instructions from here:

http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29

Then I copied source code into my class:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

So, first part of imports is ok...standard Java imports. But problem is second group. For every import, starting from:

import org.java_websocket.WebSocket;

I'm getting error "Import.... can not be resolved." And rest of the code that's using those classes is shooting errors because of that of course.

So, I added jar file to build path, but I still can't import classes from that library. What's missing here?

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • The problem is the jar you provided doesn't have those classes inside of it (WebSocket, WebSocketImpl etc). – Adz Jan 31 '15 at 14:17
  • What I have to do then? How can I add websocket library to my project? – MilanG Jan 31 '15 at 14:32
  • It depends, there's many libraries. I use Tyrus; http://mvnrepository.com/artifact/org.glassfish.tyrus – Adz Jan 31 '15 at 14:33
  • But what's the workflow? If I want to add that websocked, I mentioned above is it enough to add jar file and add it to a build path? And what's the purpose of build file if it doesn't contain those classes? Am I using wrong jar file or what? – MilanG Jan 31 '15 at 14:36
  • When I change extension of jar file to zip I see structure there: org/java_websocket/WebSocket.java and iside that file there is WebSocket interface. – MilanG Jan 31 '15 at 14:45
  • Yeah my bad I accidentally downloaded the file from the other download link which had no classes in it. – Adz Jan 31 '15 at 14:49

2 Answers2

0

First, extract the jar from the zip file, then right click the folder you're working on in your Eclipse. Click "Build path", then click "Configure Build Path", then click on "Libraries" and on the right hand side click on "Add external Jars", and then find your extracted jar.

Adz
  • 2,809
  • 10
  • 43
  • 61
  • By following that tutorial I already added it. So I had path to jar file under Build Path/Libraries as "local path" (don't know right expression). It was added as "chat/lib" ("chat" is name of my project). But I tried what you suggested and now I have the same jar file listed twice. Only after adding it your way path is "windows path": "D:\eclipse\workspace\chat\lib". And that didn't help. – MilanG Jan 31 '15 at 15:19
  • Is this the only way for adding library to eclipse? Maybe there is some other way? Inside jar file there is META-INF dir and inside it there is MANIFEST.MF file: Manifest-Version: 1.0 Built-By: david Build-Jdk: 1.7.0_15 Created-By: Apache Maven Archiver-Version: Plexus Archiver – MilanG Jan 31 '15 at 15:21
  • Sorry for the late reply, try this; http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse – Adz Feb 02 '15 at 10:55
  • I'm marking this as correct answer, because it is, but in my specific case it didn't helped me. However, I switched to NetBeans, found an example made specially for net beans and it worked like a charm. To bad there are no good websocket totorials for Eclipse. Thanks any way. – MilanG Mar 06 '15 at 08:04
-1

Download the bundle from the below repository.

MVN Repository

This should resolve your issues.

Pirate X
  • 3,023
  • 5
  • 33
  • 60