Reputation: 67
I am trying to run an image compression code in Java, and after a long time I am not using an IDE to do so, and am running the code from windows cmd itself.
Here's my code:
import java.io.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
public class Compression {
public static void main(String[] args) throws IOException {
File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("compress.jpg");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
}
}
In cmd I am in the directory where the Compression.java is present. This is what is happening:
Any suggestions? I have tried all the suggestions given on Stack on similar questions but none of them seem to work for me.
Upvotes: 1
Views: 477
Reputation: 159086
Assuming you showed the full code, your class is not in a package, so that's not the issue.
Your issue is likely that the current directory is not in the classpath, by default. Check it with this command:
set CLASSPATH
To run your code directly, use:
java -cp . Compression
This will run Java with the current directory as the only path in the classpath.
Upvotes: 2