I have the following code generated by Eclipse (.java file).
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
public class HelloWorldSWT {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello world!");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
Now I want to compile the above file from the command line. I went to the directory where the source code is located and I tried two commands:
1. javac HelloWorldSWT.java
2. javac -d /home/myname/workspace/ HelloWorldSWT.java
In both cases I have the same error "The import org.eclipse cannot be resolved".
/home/myname/workspace/ - is the directory where the class file is located.
As far as I understand the compiler does not see the org.eclipse.swt package. Why?
Can it be because the problematic package is located in "/home/myname/workspace/org.eclipse.swt/" (not in "/home/myname/workspace/org/eclipse/swt/")?
You need to set your classpath so that the Java compiler knows where to find the org.eclipse.* classes. You can do that with a command line switch or an environment variable.
Ok, Stephen C
I did this job by hand.
I used only Notepad++ (I promise)
Start Notepad++ and create file HelloWorldSWT.java
Copy example from author
Save it!
Open cmd and go to the directory with HelloWorldSWT.java
Run the command javac HelloWorldSWT.java
Ok, go to the Eclipse directory and find the correct jar swt-3.4.2-win32-win32-x86.jar
Run this again
D:\workspaces\spf_workspace\hand-made>javac -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar" HelloWorldSWT.java
All process take 2 minutes.
Don't try to run this:
`D:\workspaces\spf_workspace\hand-made>java -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar;." HelloWorldSWT`
Note: I add current dir . to classpath too.
Since you are doing Eclipse RCP development, you should let Eclipse handle your compilation as well. (You will most likely find your classes in a "build" or "bin" directory in the project).
In addition to compilation, there will be some "packaging" steps to create the final application, and Eclipse has tools for that, too.
If you really want to build outside of Eclipse, you need to manage a potentially large list of dependencies (such as org.eclipse.swt.widgets), which makes a pure javac unfeasible. You would need to look at Ant or Maven.
Also note that you will need the classpath to include dependencies not only for compilation, but also when you run the program.
But I though that I specify the
"classpath" during the compilation
(using -d option). I though that after
the "-d" option I put the name of
directory where all my packages are
located. Do I understand that wrongly?
try
javac -help
to see what the different command line options do. also note the other post above that explains this.
compiling from the command line and setting up classpath and everything right is a pain. however, it is useful to do it so that you understand what the ide actually does when it automates this for you.
The classpath variable or command line switch needs to point to where the org.eclipse.swt.widgets.Shell class resides, if this class is inside a jar file, then the classpath needs to contain the actual jar file,
i.e. javac -classpath /root/to/jar/eclipse.jar
Otherwise, if the org.eclipse.swt.widgets.Shell class is just a loose class file (which I doubt, I assume it will be inside one of the eclipse jar files, which you can list using jar -tvf jar-you-think-it-might-be-in.jar)...then you will need the javac -classpath to point to the location of the top level directory within the org/eclipse/swt/widgets/ path.
#Roman - this problem is too complicated for a beginner to try to address. The problem is that SWT has complicated dependencies, including dependencies on native code libraries.
You are best off running your SWT application using Eclipse "RunAs" ... or trying to find some Eclipse-specific documentation on running SWT-based applications from the command line.
You forgot about classpath
Related
I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.
I have a very simple piece of code I am trying to run on the Windows command line (Windows 7). It runs in Eclipse fine.
I have read How to make javac find JAR files? (Eclipse can see them)
and
https://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html#Understanding
but clearly am missing something or misunderstanding something.
Here's the code:
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.common.notify.Notifier;
public class MakeUniqIDs {
public static void main(String[] args) {
for (int i=0; i<10; i++){
System.out.println(EcoreUtil.generateUUID());
}
}
}
When I try to compile it with javac I get following error message: "MakeUniqIDs.java:1: error: package org.eclipse.emf.ecore.util does not exist"
I am in the src directory where the above code lives, and used the following to attempt to compile it:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I put the jar files in the lib directory, and also tried putting the path to the eclipse plugins directory into the classpath, but still no go.
You have to specify external JAR on the classpath.
java -cp path/some.jar; etc.
You are using classes that are part of Eclipse itself. You could dig out their JAR, but that's generally not a good idea. generateUUID() seems to be used to create a UUID in source file based on the file content, which is an IDE feature.
First ensure that the jar is in that path, and the name is exactly the same ls ..\lib or dir ..\lib. Then use the command (with the right path):
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar" MakeUniqIDs.java
Also note that you have imported org.eclipse.emf.common.notify.Notifier;, and that class is in the jar org.eclipse.emf.common not ecore, you can remove the line (since you are not using it) or add the jar separated with ;.
Example:
javac -classpath "..\lib\org.eclipse.emf.ecore_2.13.0v28170609-0707.jar;..\lib\org.eclipse.emf.common_2.13.0v28170609-0707.jar" MakeUniqIDs.java
I read the Java tutorials on Sun for JAR files, but I still can't find a solution for my problem. I need to use a class from a jar file called jtwitter.jar, I downloaded the file, and tried executing it (I found out yesterday that .jar files can be executed by double clicking on them) and Vista gave me an error saying "Failed to load Main-Class Manifest attribute from [path]/jtwitter.jar".
The guy who coded the .jar file wants me to import it, but where do I store the .jar file to import it in my code? I tried putting both the .jar file and my .java file in the same directory, didn't work.
The file I'm trying to work for is here: http://www.winterwell.com/software/jtwitter.php
I'm using JCreator LE.
Let's say we need to use the class Classname that is contained in the jar file org.example.jar
And your source is in the file mysource.java Like this:
import org.example.Classname;
public class mysource {
public static void main(String[] argv) {
......
}
}
First, as you see, in your code you have to import the classes. To do that you need import org.example.Classname;
Second, when you compile the source, you have to reference the jar file.
Please note the difference in using : and ; while compiling
If you are under a unix like operating system:
javac -cp '.:org.example.jar' mysource.java
If you are under windows:
javac -cp .;org.example.jar mysource.java
After this, you obtain the bytecode file mysource.class
Now you can run this :
If you are under a unix like operating system:
java -cp '.:org.example.jar' mysource
If you are under windows:
java -cp .;org.example.jar mysource
Not every jar file is executable.
Now, you need to import the classes, which are there under the jar, in your java file. For example,
import org.xml.sax.SAXException;
If you are working on an IDE, then you should refer its documentation. Or at least specify which one you are using here in this thread. It would definitely enable us to help you further.
And if you are not using any IDE, then please look at javac -cp option. However, it's much better idea to package your program in a jar file, and include all the required jars within that. Then, in order to execute your jar, like,
java -jar my_program.jar
you should have a META-INF/MANIFEST.MF file in your jar. See here, for how-to.
You need to add the jar file in the classpath. To compile your java class:
javac -cp .;jwitter.jar MyClass.java
To run your code (provided that MyClass contains a main method):
java -cp .;jwitter.jar MyClass
You can have the jar file anywhere. The above work if the jar file is in the same directory as your java file.
You need to put the .jar file into your classpath when compiling/running your code. Then you just use standard imports of the classes in the .jar.
As workmad3 says, you need the jar file to be in your classpath. If you're compiling from the commandline, that will mean using the -classpath flag. (Avoid the CLASSPATH environment variable; it's a pain in the neck IMO.)
If you're using an IDE, please let us know which one and we can help you with the steps specific to that IDE.
I've downloaded two jars. I want to import some of their contained classes. How do I do this?
It's for my algorithms class. I've tried following the instructions on the following site to no avail.
http://algs4.cs.princeton.edu/code/
There's an installer for OSX (I'm running Mountain Lion) which allegedly adds the jars to your classpath. Unfortunately it also installs Dr. Java. I'd rather just use Sublime and Terminal. I assumed it would be easy enough just...
import java.stdlib;
in my Percolation.java file, but javac-ing that program yields a "package stdlib does not exist", as does
import stdlib;
I've added the location of stdlib.jar and algs4.jar to my Terminal CLASSPATH manually via:
export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar/algs4.jar:/Users/Michael/path/to/jar/algs4.jar
export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar/stdlib.jar:/Users/Michael/path/to/jar/stdlib.jar
I've also attempted
javac -cp $CLASSPATH:/Users/Michael/path/to/jar/algs4.jar:/Users/Michael/path/to/jar/stdlib.jar Percolation.java
But I still get a
javac Percolation.java
Percolation.java:1: cannot find symbol
symbol : class stdlib
location: package java
import java.stdlib;
^
Percolation.java:2: package java.algs4 does not exist
import java.algs4.WeightedQuickUnionUF;
^
What's going on here?
Also is there a way to permanently add those values to my CLASSPATH in OS X mountain lion. I have to perform that command with every new Terminal.
If you're using Terminal to compile and launch your program, then in the Terminal window, begin by setting the CLASSPATH:
$ export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar
Then you can type echo $CLASSPATH and see that the jars are referenced.
Now, in the same Terminal window, use javac to compile your class. Setting the CLASSPATH as above only applies to the current Terminal window and any processes launched from it.
Alternately you can pass the CLASSPATH to javac:
$ javac -cp $CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar MyClass.java
To persist this CLASSPATH for future Terminal sessions, add the export line above to the file .profile in your home directory.
It is outdated to answer this question, but maybe it will be useful for future participants of Princeton Algorithms course.
After adding CLASSPATH in environment java get classes from packages but still will generate errors on import command. You need to delete import algs4 and stdlib from source files and compilation will run smoothly.
This solution works on Ubuntu 12.04 with zsh.
You probably have the classpath stuff right. The class you're trying to import may not be called java.stdlib though. You need to import the fully qualified package name ... probably something like org.somecompany.ourlibrary.stdlib. Thus you would need
import org.somecompany.ourlibrary.stdlib
at the top of your Percolations.java file with the rest of the import statements.
Is your Percolation program contained in its own package? If so try putting it into the default package by commenting out any package statements from your files and recompiling it.
Also, nothing in algs4 is in the java package, it's all it's own separate thing.
If you're using Eclipse (as I do), select the current project, then you open the project properties from the menus. On the left you select "Java Build path", and then you select the tab libraries. Now you click the button "Add external Jars" and you point to your jar files, and you're done.
Good luck.
Launch javac with the -classpath <path_to_jar> option. Or edit the CLASSPATH environment variable so that it contains the JAR with the classes you wish to use.
I faced the same problem during work on this course, but for windows. I'll leave this comment here in case it will help somebody.
If you use DrJava you don't need any import statements in code. If you followed installation steps described in course, everything is configured for you.
But here can be a problem - it puts jar files in your current user directory, path to which can contain inappropriate symbols(russian letters in my case). You need to check it in Edit -> Preferences menu. You can see there algs4.jar and stdlib.jar paths. Make sure that this path are correct and point to real existing files.
I just moved all necessary files to another dir and changed paths in this menu. It solved this problem for me.
I wasted a lot of time with importing the class, tried the CL option of "javac -cp .;stdlib.jar mad.java" etc but used to get the same error you mentioned.
I then commented out the import altogether and made sure the DrJava's preferences had the 2 classpaths added + the %CLASSPATH% variable to have the right value. Is simply works now.
Good luck!
just name the package to default then it will work fine . also after that you dont need to import anyting just run some code provided in
Fundamentsls
chapter like
average
.
My solution was to add 2 new build systems to my Sublime text editor: one to compile and the other to execute. Use Tools->Build_system->New_build_system... from main menu with these two code snippets:
for compilation (I've named the file "algs-compile.sublime_build"):
{
"cmd": ["javac", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
for running ("algs-run.sublime_build")
{
"cmd": ["java", "-cp", "/Users/admin/algs4/stdlib.jar:/Users/admin/algs4/algs4.jar:.", "$file_base_name"]
}
Don't forget to replace paths to jar-files here with the correct ones from your system.
I understand that this question is rather old but I hope this will help somebody.
If you are getting the "cannot be resolved to a type" error, and have tried adding the stdlib.jar or algs4.jar files, here is the solution:
The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use the libraries with a named package, you can use these package versions:
stdlib-package.jar and algs4-package.jar.
You can download these files here:
http://algs4.cs.princeton.edu/code/
Then you can automatically add the import:
import edu.princeton.cs.algs4.ClassName
Open your Sublime
Choose the Tools->Build System->New Build System
Add below code to the new file
This can tell the sublime to run the commands
{
"cmd": ["javac -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file\" && java -cp java -cp /Users/yourusername/algs4/stdlib.jar:/Users/yourusername/algs4/algs4.jar:. \"$file_base_name\""],
"shell":true,
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
Hope this can help those who are following the Algorithm Course from Princeton University
I had this same problem. Renfei Wang's solution worked for me (I don't have enough points yet to comment directly on his response).
In Sublime, navigate to Preferences: Browse Packages>JavaC.sublime_build.
Here's what mine looks like:
{
"cmd": ["javac", "-cp", "/Users/jason/Documents/lib/*:./","$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}
/Users/jason/Documents/lib/*:/ lets Sublime know the location of the directory that holds my packages, so that now when I build, it loads those packages first.
Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )