0

I am trying to make a jar file for handle webcam. I have to use this jar to another application. But this application made by java 1.6 so I have to convert this jar to 1.6. this code is ok for java 8. when I am trying to compile it by java 1.6 it given this this error

Updating property file: F:\Core Java\Camera\build\built-jar.properties
Compiling 1 source file to F:\Core Java\Camera\build\classes
compile-single:
run-single:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/github/sarxos/webcam/WebcamPanel : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at ati.com.camera.CaptureImageMP.main(CaptureImageMP.java:53)
Java Result: 1

Please hep me how can i run this project by java 1.6

This is the code for capture image

public class CaptureImageMP extends VBean {

private IHandler mHandler;
private Main formsMain;
protected static final ID VN_NO = ID.registerProperty("VN_NO");

@Override
public void init(IHandler handler) {
    mHandler = handler;
    super.init(handler);
    formsMain = (Main) mHandler.getApplet();
}

@Override
public boolean setProperty(ID _ID, Object _args) {

    System.out.println("Method Called");
    if (_ID == VN_NO) {
        System.out.println("Got VN No" + VN_NO);
        System.out.println("Got _ID No" + _ID);
        if (_args != null) {
            System.out.println("Got parameter :" + _args);
            try {
             new TakeSnapshotFromVideoExample();
            } catch (Exception ex) {
                System.out.println("Exception: " + ex);
                return false;
            }
        }

        return true;
    }

    //return true;
    return super.setProperty(_ID, _args);
}
public static void main(String[] args) throws InterruptedException {
    new TakeSnapshotFromVideoExample();
          //  Thread.sleep(1000);
          //  new Game1();
}

this is the code for capture image and on/off camera UI

@SuppressWarnings("serial") public class TakeSnapshotFromVideoExample extends JFrame {

private class SnapMeAction extends AbstractAction {

    public SnapMeAction() {
                
        super("Snapshot");
            }

    @Override
    public void actionPerformed(ActionEvent e) {
            //    int unique_id= (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE); 
          int unique_id= (int) (Integer.MAX_VALUE); 
        try {
            for (int i = 0; i < webcams.size(); i++) {
                Webcam webcam = webcams.get(i);
                //File file = new File(String.format("Image\\"+unique_id+".jpg", i));
                                    File file = new File(String.format("F:\\Core Java\\camera\\ATILimitedRegImage\\"+unique_id+".jpg", i));
                ImageIO.write(webcam.getImage(), "JPG", file);
                System.out.format("Image for %s saved in %s \n", webcam.getName(), file);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

private class StartAction extends AbstractAction implements Runnable {

    public StartAction() {
        super("Start");
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        btStart.setEnabled(false);
        btSnapMe.setEnabled(true);

        // remember to start panel asynchronously - otherwise GUI will be
        // blocked while OS is opening webcam HW (will have to wait for
        // webcam to be ready) and this causes GUI to hang, stop responding
        // and repainting

        executor.execute(this);
    }

    @Override
    public void run() {

        btStop.setEnabled(true);

        for (WebcamPanel panel : panels) {
            panel.start();
        }
    }
}

private class StopAction extends AbstractAction {

    public StopAction() {
        super("Stop");
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        btStart.setEnabled(true);
        btSnapMe.setEnabled(false);
        btStop.setEnabled(false);

        for (WebcamPanel panel : panels) {
            panel.stop();
        }
    }
}

private Executor executor = Executors.newSingleThreadExecutor();

//private Dimension size = WebcamResolution.QQVGA.getSize();
    private Dimension size = WebcamResolution.VGA.getSize();

private List<Webcam> webcams = Webcam.getWebcams();
private List<WebcamPanel> panels = new ArrayList<WebcamPanel>();

private JButton btSnapMe = new JButton(new SnapMeAction());
private JButton btStart = new JButton(new StartAction());
private JButton btStop = new JButton(new StopAction());

public TakeSnapshotFromVideoExample() {

    super("Test Snap Different Size");

    for (Webcam webcam : webcams) {
        webcam.setViewSize(size);
        WebcamPanel panel = new WebcamPanel(webcam, size, false);
        //panel.setFPSDisplayed(true);
        panel.setFillArea(true);
        panels.add(panel);
    }

    // start application with disable snapshot button - we enable it when
    // webcam is started

    btSnapMe.setEnabled(false);
    btStop.setEnabled(false);

    setLayout(new FlowLayout());

    for (WebcamPanel panel : panels) {
        add(panel);
    }

    add(btSnapMe);
    add(btStart);
    add(btStop);

    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TakeSnapshotFromVideoExample();
}

Note: i am using Netbeans IDE

2 Answers2

1

major version number of the class file format being used.

Java SE 17 = 61 (0x3D hex),

Java SE 16 = 60 (0x3C hex),

Java SE 15 = 59 (0x3B hex),

Java SE 14 = 58 (0x3A hex),

Java SE 13 = 57 (0x39 hex),

Java SE 12 = 56 (0x38 hex),

Java SE 11 = 55 (0x37 hex),

Java SE 10 = 54 (0x36 hex),

Java SE 9 = 53 (0x35 hex),

Java SE 8 = 52 (0x34 hex),

Java SE 7 = 51 (0x33 hex),

Java SE 6.0 = 50 (0x32 hex),

Java SE 5.0 = 49 (0x31 hex),

JDK 1.4 = 48 (0x30 hex),

JDK 1.3 = 47 (0x2F hex),

JDK 1.2 = 46 (0x2E hex),

JDK 1.1 = 45 (0x2D hex).

Source: Wikipedia

Analysis of error message

Based on the error message com/github/sarxos/webcam/WebcamPanel : Unsupported major.minor version 51.0,

  1. there are classes(jars) compiled to target 1.7
  2. when used with JRE lesser than 1.7, it will result in 51.0 error

Some guesses

Assuming you are using the jars generated from this repo github:sarxos (again based on the error message),

  1. this repo was compiled to target 1.7
  2. the author probably uses 1.7 language features
  3. the author probably expects users of his jar to run on 1.7 and above

Options

If the assumptions were correct so far, the possible options are

  1. upgrade your runtime to at-least 1.7
  2. compile all the sources(including the sources of transitive jars with target 1.6) - assuming we have access to the sources
  3. try to use some retro byte code modifier utility

1. Upgrade

If upgrade is an option, then please upgrade to 1.7(recommended to 1.8 or even 11) as target 1.6 compiling of all the dependent sources is too much an effort required than upgrading to 1.8(or above) for an enterprise application.

2. Compile sources to target 1.6

If upgrade is not possible, then if this external jar is used, then

  1. fork the repo(or checkout)*(please check the licensing of this author),
  2. compile the forked repo to target 1.6
  3. include this jar in the application and compile the application to target 1.6
  4. deploy this fully 1.6 target compiled jar to 1.6 runtime

3. Some bytecode magic

my knowledge is very limited and can't suggest anything concrete. you can checkout this repo

Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
-1

Well, the library requires that you compiled the classes in JDK 1.6 or to version 1.6

At compile time, use source and target versions as below. Let me know if it works.

javac -source 1.6 -target 1.6