Importing has nothing to do with loading classes or setting CLASSPATH.
Try this:
java -cp .;../lib/* Generator
Using the dot '.' as the first entry in the CLASSPATH assumes that the Generator.class file exists in the directory from which you're running java, and /lib is one level up from that directory. Adjust as needed if both of these are not correct.
postgresql - Running a java program in linux terminal with -class path - Stack Overflow
java - adding classpath in linux - Stack Overflow
How to set Java classpath in Linux? - Stack Overflow
mysql - How to run java program in linux terminal using classpath - Stack Overflow
Videos
Importing has nothing to do with loading classes or setting CLASSPATH.
Try this:
java -cp .;../lib/* Generator
Using the dot '.' as the first entry in the CLASSPATH assumes that the Generator.class file exists in the directory from which you're running java, and /lib is one level up from that directory. Adjust as needed if both of these are not correct.
You should run the program including again the same cp:
java -cp "lib directory where i put all the jars" MainClassOfYourApplication
After you compiled it with:
javac -cp "lib directory where i put all the jars" AvroReader.java
More applied to your example:
First step(compile all the needed java files): javac -cp "path/to/jars/*" AvroReader.java //here you should include all the java files not yet compiled but which you need to run your app
Second step: java -cp "path/to/jars/*" package.subpackage1.subpackage2.Generator
When you specify the classpath you need to make sure it includes ALL of the class files your application needs, including the ones you create yourself. Assuming that Test.class is in the current directory along with the postgres Jar file, you need something like:
java -classpath postgresql-9.0-801.jdbc4.jar:. Test
See the Java Glossary for more details.
bm~
What is the error? ClassNotFoundException for Test or the postgres library? If former, it is because you need to add Test in your classpath as well.
Assuming you are in the same directory where Test.class and the postgres jar is present,
java -classpath .:postgresql-9.0-801.jdbc4.jar Test
I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:
- If you modify it in one script, suddenly some java programs will stop working.
- If you put there the libraries for all the things which you run, and it gets cluttered.
- You get conflicts if two different applications use different versions of the same library.
- There is no performance gain as libraries in the CLASSPATH are not shared - just their name is shared.
- If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place - that will cause confusion, for sure.
Therefore the preferred way is to set the classpath per each run of the jvm, for example:
Copyjava -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" "folder.subfolder../dit1/some.xml
If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.
It's always advised to never destructively destroy an existing classpath unless you have a good reason.
The following line preserves the existing classpath and adds onto it.
Copyexport CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"
export CLASSPATH=/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar
or, if you already have some classpath set
export CLASSPATH=$CLASSPATH:/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar
and, if also you want to include current directory
export CLASSPATH=$CLASSPATH:/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar:.
You have to use ':' colon instead of ';' semicolon.
As it stands now you try to execute the jar file which has not the execute bit set, hence the Permission denied.
And the variable must be CLASSPATH not classpath.
I'm not a Java programmer, so I don't know the correct value for CLASSPATH. But, you seem to. You can add it to ~/.bashrc like so:
CLASSPATH=/path/to/1:/path/to/2:/etc
The change will take effect globally the next time you log in. However, it will take effect immediately in new shells.
Additionally, if you want to set it for just one particular command, do this:
CLASSPATH=/something command-here arg1 arg2
A third way would be to create a wrapper script, which would be appropriate if you needed to set multiple variables or if you needed to determine appropriate values programmatically:
#!/bin/bash
export CLASSPATH=/something
export ANOTHER_ENV_Variable=foo
exec your_fancy_program "$@"
that's what happened with me.. i left empty spaces while typing.. it fixed when i removed spaces.
1) if you want to set classpath permanently then 1) find out where java is installed.. you may use " whereis java " openjdk-7/6 is in /usr/lib/jvm/.....
2) we need to set up CLASSPATH in /etc/environment
sudo gedit /etc/environment
3) add the following likes .. ( DONT LEAVE ANY SPACES WHILE TYPING)(customize according to your java version and installation) (this home path is for open jdk 7)
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/java-7-openjdk-i386/lib:/home/laptop/Desktop/a2"
export CLASSPATH
separate directory by ":"
I think you should not put any paths that a local to your home directory in a system wide file. I would leave /etc/environment well alone, unless you provide some changes, that are necessary or beneficial to all users.
Put any changes to the CLASSPATH in your .bashrc in your home directory.
CLASSPATH=$CLASSPATH:/home/foo:/home/foo/Java_code/my_code
export CLASSPATH
This way you can source it and any newly started bash will have the settings right at once.
export CLASSPATH=""
or better
unset CLASSPATH
will delete an existing Classpath. There are multiple locations where you can set or unset the classpath - a missing entry will not unset it.