ClassPool.getDefault(); does nothing in Javassist - java

public byte[] transform(ClassLoader loader, String className, Class<?> clazz,
ProtectionDomain domain, byte[] bytes)
throws IllegalClassFormatException {
return inspectClass(className, clazz, bytes);
}
private byte[] inspectClass(String name, Class<?> clazz, byte[] b) {
System.out.println("here"); //OK I see this print
ClassPool pool = ClassPool.getDefault();
System.out.println("inclass"); //can't see it !!
}
What can happen in ClassPool.getDefault();?

I had the same problem, and found ClassPool.getDefault was not throwing Exception, but Throwable.
In fact, it was throwing java.lang.NoClassDefFoundError.
In my manifest, I had:
Premain-Class: timing.TimingTransform
Boot-Class-Path: lib/javassist.jar
You likely just need to point the Boot-Class-Path to the javassist.jar file.
In my case, with the Boot-Class-Path above, I needed a lib directory with javassist.jar in it.
The mistake I made initially was putting javassist.jar inside the agent jar file
(THE FOLLOWING IS INCORRECT, FOR DEMONSTRATION PURPOSES ONLY):
0 Mon Oct 24 16:58:14 MST 2011 META-INF/
146 Mon Oct 24 16:58:14 MST 2011 META-INF/MANIFEST.MF
0 Thu Oct 20 14:58:06 MST 2011 timing/
2482 Mon Oct 24 16:58:06 MST 2011 timing/TimingStats.class
8360 Mon Oct 24 16:58:06 MST 2011 timing/TimingTransform.class
0 Tue Oct 18 17:28:24 MST 2011 lib/
645252 Fri Jul 08 18:24:58 MST 2011 lib/javassist.jar
Rather than putting javassist.jar inside the agent jar file, I put it in an external directory accessible to the program. After that change, it worked fine.

Related

ZipException thrown when using jogl in eclipse

This is Eclipse project build path:
files in project:
rob#work:~/git/thegame$ ll lib/linux32/
total 708
drwxr-xr-x 2 rob rob 4096 Mar 22 02:37 ./
drwxr-xr-x 4 rob rob 4096 Mar 22 02:23 ../
-rw-r--r-- 1 rob rob 8704 Mar 10 14:00 libgluegen-rt.so
-rw-r--r-- 1 rob rob 666380 Mar 11 03:22 libjogl_desktop.so
-rw-r--r-- 1 rob rob 5944 Mar 11 03:22 libnativewindow_awt.so
-rw-r--r-- 1 rob rob 26604 Mar 11 03:22 libnativewindow_x11.so
rob#work:~/git/thegame$ ll jar/
total 3308
drwxr-xr-x 3 rob rob 4096 Mar 22 02:28 ./
drwxr-xr-x 8 rob rob 4096 Mar 22 02:22 ../
-rw-r--r-- 1 rob rob 289171 Mar 10 14:00 gluegen-rt.jar
drwxr-xr-x 4 rob rob 4096 Mar 22 02:28 javadocs/
-rw-r--r-- 1 rob rob 3082066 Mar 11 03:23 jogl-all.jar
Error when trying to execute application:
Catched ZipException: error in opening zip file, while addNativeJarLibsImpl(classFromJavaJar class com.jogamp.common.os.Platform, classJarURI jar:file:/home/rob/git/thegame/jar/gluegen-rt.jar!/com/jogamp/common/os/Platform.class, nativeJarBaseName gluegen-rt-natives-linux-i586.jar): [ file:/home/rob/git/thegame/jar/gluegen-rt.jar -> file:/home/rob/git/thegame/jar/ ] + gluegen-rt-natives-linux-i586.jar -> slim: jar:file:/home/rob/git/thegame/jar/gluegen-rt-natives-linux-i586.jar!/
Catched ZipException: error in opening zip file, while addNativeJarLibsImpl(classFromJavaJar class jogamp.nativewindow.NWJNILibLoader, classJarURI jar:file:/home/rob/git/thegame/jar/jogl-all.jar!/jogamp/nativewindow/NWJNILibLoader.class, nativeJarBaseName jogl-all-natives-linux-i586.jar): [ file:/home/rob/git/thegame/jar/jogl-all.jar -> file:/home/rob/git/thegame/jar/ ] + jogl-all-natives-linux-i586.jar -> slim: jar:file:/home/rob/git/thegame/jar/jogl-all-natives-linux-i586.jar!/
Catched IOException: TempJarCache: addNativeLibs: jar:file:/home/rob/git/thegame/jar/jogl-all-natives-linux-i586.jar!/, previous load attempt failed, while addNativeJarLibsImpl(classFromJavaJar class jogamp.nativewindow.NWJNILibLoader, classJarURI jar:file:/home/rob/git/thegame/jar/jogl-all.jar!/jogamp/nativewindow/NWJNILibLoader.class, nativeJarBaseName jogl-all-natives-linux-i586.jar): [ file:/home/rob/git/thegame/jar/jogl-all.jar -> file:/home/rob/git/thegame/jar/ ] + jogl-all-natives-linux-i586.jar -> slim: jar:file:/home/rob/git/thegame/jar/jogl-all-natives-linux-i586.jar!/
Catched IOException: TempJarCache: addNativeLibs: jar:file:/home/rob/git/thegame/jar/jogl-all-natives-linux-i586.jar!/, previous load attempt failed, while addNativeJarLibsImpl(classFromJavaJar class jogamp.nativewindow.NWJNILibLoader, classJarURI jar:file:/home/rob/git/thegame/jar/jogl-all.jar!/jogamp/nativewindow/NWJNILibLoader.class, nativeJarBaseName jogl-all-natives-linux-i586.jar): [ file:/home/rob/git/thegame/jar/jogl-all.jar -> file:/home/rob/git/thegame/jar/ ] + jogl-all-natives-linux-i586.jar -> slim: jar:file:/home/rob/git/thegame/jar/jogl-all-natives-linux-i586.jar!/
Main.java
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class Main
{
public static void main(String[] args)
{
// setup OpenGL Version 2
GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
// The canvas is the widget that's drawn in the JFrame
GLCanvas glcanvas = new GLCanvas(capabilities);
glcanvas.addGLEventListener(new Renderer());
glcanvas.setSize( 300, 300 );
JFrame frame = new JFrame( "Hello World" );
frame.getContentPane().add( glcanvas);
// shutdown the program on windows close event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
frame.setSize( frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
}
}
When you download the JARs directly, some web browsers might wrap it into a ZIP file for "security" reasons. Rather download the 7z archive here and take the JARs it contains. You should follow these detailed instructions, it should work in Eclipse.
I remind you that the separated native libraries are no longer required in JOGL 2, rather use the JARs containing the native libraries, it is a lot less error prone, just put them into the same directory than the Java libraries relying on them (jogl-all.jar and gluegen-rt.jar).

write logs to oldest file in directory

I want to do some tweaks to my logging for my application...
I would like some help to enhance what I have below in main method:
public static void main(String[] args) {
try {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Handler h = new FileHandler("../logs/MyLogFile_"
+ sdf.format(date) + ".log", true);
h.setFormatter(new SingleLineFormatter());
h.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
logger.addHandler(h);
}
//...
}
It creates a log file with date stamp everytime I run the application. But I want to achieve something like this in my Unix Directory:
-rw-r--r-- 1 r787848 dev 45271 Feb 4 11:31 MyLogFile.log.06
-rw-r--r-- 1 r787848 dev 45308 Feb 5 11:36 MyLogFile.log.05
-rw-r--r-- 1 r787848 dev 44336 Feb 6 06:50 MyLogFile.log.04
-rw-r--r-- 1 r787848 dev 44379 Feb 7 08:41 MyLogFile.log.03
-rw-r--r-- 1 r787848 dev 44409 Feb 10 08:45 MyLogFile.log.02
-rw-r--r-- 1 r787848 dev 44446 Feb 11 12:36 MyLogFile.log.01
I want to define a set of lets say 6 log files to capture logging of daily run of the application. When it comes to logging, I want the application to write to the log file that is oldest, so in the above instance, running the application on Feb 12 08:45 should clear MyLogFile.log.06 and write fresh for feb 12.
How can this be achieved with java.util.logging on top of what I have. Unfortunately, I am not able to configure log4j properties and want to use java.util.logging only.
The only close approximation is to do the following:
Handler h = new FileHandler("../logs/MyLogFile_"
+ sdf.format(date) + ".log", Integer.MAX_VALUE, 6, false);
See: JDK-6350749 - Enhance FileHandler to have Daily Log Rotation capabilities.

Java - Read specific files in specific order from a folder [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my Java program i process a certain amount of files.
Those Files are named in this way:
thu 21 mar 2013_01.55.22_128.txt
thu 21 mar 2013_01.55.22_129.txt
thu 21 mar 2013_01.55.22_130.txt
....
sat 23 mar 2013_01.45.55_128.txt
sat 23 mar 2013_01.45.55_129.txt
sat 23 mar 2013_01.45.55_130.txt
Where the last three numbers are the cell number.
Consider that i already read in order of date the files coming from the same cell.
Consider that all the files are on the same folder.
Consider also that this problem, but for a single cell, was correctly solved on This Post
My question now is: how can i read first all the txt coming from a specific cell (e.g 128), then all the files coming from cell 129 and so on? (below: a graphic example)
thu 21 mar 2013_01.55.22_128.txt
sat 23 mar 2013_01.45.55_128.txt
...
thu 21 mar 2013_01.55.22_129.txt
sat 23 mar 2013_01.45.55_129.txt
...
thu 21 mar 2013_01.55.22_130.txt
sat 23 mar 2013_01.45.55_130.txt
I hope I was clear
You may get all files in directory using listFiles() into array then sort it using custom comparator.
File[] files = dir.istFiles();
Array.sort(files, new Comparator<File> {
#Override
public int compare(File lhs, File rhs) {
//return -1 if lhs should go before
//0 if it doesn't matter
//1 if rhs should go after
}
});
Well, you could read the folder in order to get the File objects (or maybe just file names).
Then parse the file names, extract the cell and put the files into a map whose key is the cell.
Some pseudo code:
Map<String, List<File>> filesPerCell = new LinkedHashMap<String, List<File>>();
File[] files = folder.listFiles();
for( File file : files ) {
String filename = file.getName();
String cell = ... ; //extract from filename
List<File> l = filesPerCell.get( cell );
//create new list if l is null
l.add( file );
}
for( List<File> cellList : filesPerCell.values() ) {
//do whatever you want with the files for that cell
}
You will have your file names sorted by cell number, and inside the cell, by date/time. You could do this most easily, if your file names were like this:
cellnumber_yyyymmdd_hhmmss
where cellnumber would be the same number of digits in all cases.
Otherwise you must write a custom comparator (as #RiaD writes), but it is not trivial because of the dates that must be parsed so one could decide on later/earlier.

adding methods to existing java class and recompile with my project

I'm trying to do something that should be pretty easy but some how I keep failing...
the idea is to take existing java class from java repository (in our case java sun)
modify it a bit.. recompile the class and use the modified class in our project
the steps (I took String.java from java.lang for example)
modify String String.java by adding:
public int zzz() {
return 123;
}
just under the class constructors.
recompile String.java
javac -d String String.java
jar -cf the compiled files
this is the the result of: jar -vtf String.jar:
0 Wed May 22 10:31:06 IDT 2013 META-INF/
68 Wed May 22 10:31:06 IDT 2013 META-INF/MANIFEST.MF
9763 Wed May 22 10:30:44 IDT 2013 java/lang/String$1.class
1232 Wed May 22 10:26:04 IDT 2013 java/lang/String$CaseInsensitiveComparator.class
17269 Wed May 22 10:26:04 IDT 2013 java/lang/String.class
write short main class:
public class main {
/**
* #param args
*/
public static void main(java.lang.String[] args) {
// TODO Auto-generated method stub
java.lang.String s = new java.lang.String(" xxx ");
s = s.concat("bla bla");
System.out.println(s);
System.out.println(s.zzz());
}
}
(I get the same behavior when trying java.lang.String and just String.)
5.compile my main.java with the modified String class
javac -Xbootclasspath/p:String.jar main.java
6.run main
java -Xbootclasspath/p:String.jar main
that gives us the following output:
myusername#machinename:~/work/sand_box$ java -Xbootclasspath/p:String.jar main
xxx bla bla
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.<init>([CZ)V
at java.lang.Integer.toString(Integer.java:333)
at java.lang.Integer.toString(Integer.java:136)
at java.lang.String.valueOf(String.java:2948)
at java.io.PrintStream.print(PrintStream.java:597)
at java.io.PrintStream.println(PrintStream.java:736)
at main.main(main.java:12)
I can't figure out what am I doing wrong
can someone please shed some light on this please?
10x to all the contributors out there.
From my pt of view it would be better to just extend String instead of recompiling a modified version of a JDK class
e.g.: public class String extends java.lang.String
That way you'll create a new String class in your package
Of course, depending on your needs this may not be the best option
But in general I think it's no good idea to modify JDK classes directly - at least if you don't plan to include (and recompile) all the SDK from source by yourself
KR
Florian

Sequence Files in Hadoop

How are these sequence files generated ? I saw a link about sequence file here,
http://wiki.apache.org/hadoop/SequenceFile
Are these written using default Java serializer ? and How do I read a sequence file ?
Sequence files are generated by MapReduce tasks and and can be used as common format to transfer data between MapReduce jobs.
You can read them in the following manner:
Configuration config = new Configuration();
Path path = new Path(PATH_TO_YOUR_FILE);
SequenceFile.Reader reader = new SequenceFile.Reader(FileSystem.get(config), path, config);
WritableComparable key = (WritableComparable) reader.getKeyClass().newInstance();
Writable value = (Writable) reader.getValueClass().newInstance();
while (reader.next(key, value))
// perform some operating
reader.close();
Also you can generate sequence files by yourself using SequenceFile.Writer.
The classes used in the example are the following:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
And are contained within the hadoop-core maven dependency:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
Thanks to Lev Khomich's answer, my problem has been solved.
However, the solution has been deprecated for a while and the new API offers more features and also easy to use.
Check out the source code of hadoop.io.SequenceFile, click here:
Configuration config = new Configuration();
Path path = new Path("/Users/myuser/sequencefile");
SequenceFile.Reader reader = new Reader(config, Reader.file(path));
WritableComparable key = (WritableComparable) reader.getKeyClass()
.newInstance();
Writable value = (Writable) reader.getValueClass().newInstance();
while (reader.next(key, value)) {
System.out.println(key);
System.out.println(value);
System.out.println("------------------------");
}
reader.close();
Extra info, here is the sample output running against the data file generated by Nutch/injector:
------------------------
https://wiki.openoffice.org/wiki/Ru/FAQ
Version: 7
Status: 1 (db_unfetched)
Fetch time: Sun Apr 13 16:12:59 MDT 2014
Modified time: Wed Dec 31 17:00:00 MST 1969
Retries since fetch: 0
Retry interval: 2592000 seconds (30 days)
Score: 1.0
Signature: null
Metadata:
------------------------
https://www.bankhapoalim.co.il/
Version: 7
Status: 1 (db_unfetched)
Fetch time: Sun Apr 13 16:12:59 MDT 2014
Modified time: Wed Dec 31 17:00:00 MST 1969
Retries since fetch: 0
Retry interval: 2592000 seconds (30 days)
Score: 1.0
Signature: null
Metadata:
Thanks!

Categories

Resources