classpath issues outside of Eclipse - java

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

Related

Can a Java class import classes from a third-party project with dependencies managed by Gradle? [duplicate]

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.

Installing GSON JAR file and using it with Java [duplicate]

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.

Can't run compiled java file with main method

I have an application, which I try to build using Maven. I can build a jar, but running it does nothing: it can't find a Main class. I set however the MainClass in the POM. When I try to run the MainPane.class in the target folder from the command line returns:
"Error: could not find or load Main class MainPane". (After navigating inside the target folder and run 'java MainPane')
I can run this class from Eclipse and it has a main method:
public static void main(String[] args) {
System.out.println("test");
}
I should be able to run the class file in the target folder right? What can possibly go wrong?
You need to tell java where the class is by defining the -classpath parameter
java -classpath classes MainPane
Or from the project directory
java -classpath target/classes MainPane
Also make sure you are using the full package name if you have one
java -classpath target/classes my.package.name.MainPane
#AndyGeeDe
TomEE treibt Tomitribe! | http://tomee.apache.org
java -jar myjar.jar
Will only work if the jar's manifest contains the "Main-Class: classname" entry. Check for that. If it is not present then you can still run you application via this command:
java -cp ./my.jar package/class_name
Try googling on "maven executable jar". Once you get the build correct, you can run it using
java -jar myjar.jar
Java is not terribly good at this. The executable jar needs all the dependent jars unpacked, and a manifest file included that names the main class. To my mind is less than elegant but it does work.

Again "wrong name" error when executing java program

With reference to this post
Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line
I did not understand how to solve the problem
Actually in my java source code there' s line :
package es_2011;
when I compile the program through JCreator everything works perfectly.
It creates a folder named es_2011 where to put .class files.
Also the executing operation goes smoothly, the program runs ok.
Now I'd like to use the command line only.
So I placed my java file in the directory where javac.exe is but whenever I try to compile I get the same error
The command I use is: javac ProgAudioJ.java
The path (where javac.exe is ) is : C:\Program files\Java\jdk1.6.0_22\bin
Is someone willing to help me understand in terms of exactly tell me what I have to do?
thanks very much...MAX
The setup used for the looks like this (under windows)
C:\classDir -> is the project
C:\classDir\testpackage -> is the only package used (the package "testpackage")
C:\classDir\testpackage\Main.class -> is the class with the main method inside (important: it is the .class and not .java)
The Main.class looks like following:
package testpackage;
public class Main {
public static void main(String[] args) {
System.out.println("Program started! ;-)");
}
}
go with your command prompt to:
c:\classDir> java testpackage.Main
the result:
Program started! ;-)
According to your problems that it starts in your IDE but not from the console:
- checked if you realy use the path to the .class files?
- with the console go to the directory of you .class files, not the project (e.g. in Eclipse it is the bin directory
- enter the full qualified class name (including packages seperated by . -> e.g. testpackage.Main
More infos can be found under:
http://www.oracle.com/technetwork/java/compile-136656.html
Hope it helped
MAX, if the class defines that it's inside the package es_2011, then it should be in a folder with the same name.
So in your case, put the ProgAudioJ.java in the folder es_2011 and then run
javac es_2011\ProgAudioJ.java
latter to run it, you need the command
java es_2011.ProgAudioJ
You should add javac.exe in your path .Edit your path variable and append path to jdk's bin
then put java file in a dir named es_2011 , as the package declaration is es_2011 then compile
c:\es_2011\javac YourJava.java
and now go back to C:
c:\java es_2001.Yourjava
After reading you other Post: "Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line" I guess you go to the directory es_2011 where your ProgAudioJ.class file is located and run
java ProgAudioJ
from that folder.
instaend you have to go to the folder above (cd ..) and run
java es_2011.ProgAudioJ
Each package in Java corresponds to a folder on the filesystem. So a package declaration such as com.stackoverflow would mean that the source classes need to be in a folder ./com/stackoverflow. Typically the whole project would have a separate src folder containing com/stackoverflow.
When you compile the Java classes you DO NOT need to put source files in the same directory as javac.exe, you do however need to make sure that javac.exe is in your operating systems PATH variable. This tells the operating system where it should look for executable files when a command is run, on a *nix machine this would usually be /usr/bin or just /bin but on Windows machine the executables normally live within the applications own directories, that is C:\Program Files\something. Assuming that you've installed JDK correctly, the javac.exe should already be in the PATH you can check this by opening the command line and just running javac (just like that). If you get some output then all is well, the system knows where to find javac.exe.
Next you will need to go to your project folder and type javac -d . src/com/stackoverflow/MainSO.java notice that is run from the project folder. This will create a folder called com in your project root and put the compiled classes in com/stackoverflow. The -d flag tells javac where to put the compiled classes, if you leave that out, the compiled classes will be where the sources are.
Then when you want to run the classes you type java com.stackoverflow.MainSO (no .class). Crucially this command will need to be ran in the directory that contains the root of the class hierarchy (that is the com folder containing the compiled classes). You can specify other places for java to look for the classes by providing a classpath to the java command with the -cp flag. By default the classpath will contain the directory the java command was ran in. Should your project have dependencies external .jar files for example you will need to provide every single one of them (with their full filepath) in the classpath (this goes for the compiler as well). The IDEs do this automatically for you.
Hope that helps.

How to compile a .java file in Java?

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

Categories